1

I am trying to use setattr on a UI element (QLineEdit) to fill in with what was read from a text file. I believe in order to set a QlineEdit it would be self.lineEdit.setText()

The text file I am reading consists of a Name and it's value:

Name1=Value1

splitLine[0] consists of "Name1", and splitLine[1] is "Value1". self.Name1 is the name of the lineEdit I am changing, hence I used eval() to pass the actual value "Name1" to setattr.

I am not sure how to go about setting the value. Right now I have tried these with no success:

setattr(self, eval("splitLine[0]"), eval("splitLine[1]"))

setattr(self, eval("splitLine[0]"), setText(eval("splitLine[1]")))

Also, using:

self.splitLine[0].setText(splitLine[1])

Does not work as it thinks the actual object is called splitLine, rather than it's value (hence why I tried eval() ).

# AttributeError: 'Ui_Dialog' object has no attribute 'splitLine'
Zak44
  • 341
  • 1
  • 5
  • 24
  • What attributes are you trying to set? Most Qt objects don't have any attributes you can set. You normally have to use the accessor/mutator methods. – Brendan Abel Feb 13 '16 at 02:11
  • There doesn't seem to be any need of your `eval` calls, since you're using a constant string for each of them. Why not just use `splitline[0]` and `splitline[1]`? – Blckknght Feb 13 '16 at 02:13
  • self.splitLine[0].setText(splitLine[1]) is not valid The Qt object I am setting is: self.Name1 and splitLine[0] = "Name1" How am I to get that value out of splitLine[0] in order to set the Qt object? – Zak44 Feb 13 '16 at 02:18

2 Answers2

1

You need to use getattr, not setattr. That is, you first need to get the line-edit object (via its attribute name), so that you can then call its setText method to populate the field:

    lineEdit = getattr(self, splitLine[0])
    lineEdit.setText(splitLine[1])

or in one line:

    getattr(self, splitLine[0]).setText(splitLine[1])
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • I am trying to set a QLineEdit UI field. Why would I use getattr? I am confused. – Zak44 Feb 15 '16 at 19:13
  • @sz200. I have updated my answer to make it clearer. Did you actually try the code? – ekhumoro Feb 15 '16 at 19:28
  • Thank you @ekhumoro. But I would like to understand this. why would assigning the value to another variable lineEdit, and then calling setText on lineEdit give me the result? But say... self.splitLine[0].setText(splitLine[1]) Will not and instead I get: # AttributeError: 'Ui_Dialog' object has no attribute 'splitLine'? I just don't get it and would like to understand the logic. – Zak44 Feb 15 '16 at 20:12
  • The [getattr](http://docs.python.org/3/library/functions.html#getattr) function allows you to get an attribute by using a string corresponding to its name. The actual attribute name of your line-edit is "Name1", not "splitline[0]". You need to be able to do `self.Name1.setText(splitline[1])`. The `self.Name1` part is done by `getattr(self, splitline[0])`, where `splitline[0]` *contains* the string "Name1". – ekhumoro Feb 15 '16 at 20:39
0

There's no need for eval; splitLine contains strings, which is the required type for the second argument and sufficient for the third argument.

setattr(self, splitLine[0], splitLine[1])
chepner
  • 497,756
  • 71
  • 530
  • 681