Passing list to class init, pyQt
-
wrote on 1 Jun 2017, 09:44 last edited by
Hi All
I'm new to Python and pyQt. I've got a class for a QDialog, and am passing a list of strings to this class in the init;
class Remove_Source(QtWidgets.QDialog, Ui_R_Source): def __init__(self, items=None): super(Remove_Source , self).__init__() self.ui = Ui_R_Source() self.ui.setupUi(self) self.setWindowTitle("Remove Source Folders") self.items = items or [] for i in range(self.items.count()): self.ui.keep_window.addItem(self.items(i)) self.exec()
The list is inaccessible within the class. Anyone know what I'm doing wrong? Thanks
-
Hi All
I'm new to Python and pyQt. I've got a class for a QDialog, and am passing a list of strings to this class in the init;
class Remove_Source(QtWidgets.QDialog, Ui_R_Source): def __init__(self, items=None): super(Remove_Source , self).__init__() self.ui = Ui_R_Source() self.ui.setupUi(self) self.setWindowTitle("Remove Source Folders") self.items = items or [] for i in range(self.items.count()): self.ui.keep_window.addItem(self.items(i)) self.exec()
The list is inaccessible within the class. Anyone know what I'm doing wrong? Thanks
@robbiecooper said in Passing list to class init, pyQt:
inaccessible
What do you mean by "inaccessible"?
Also, how do you pass the list to the constructor?
And this is wrong:self.items(i)
must be
self.items[i]
-
Hi All
I'm new to Python and pyQt. I've got a class for a QDialog, and am passing a list of strings to this class in the init;
class Remove_Source(QtWidgets.QDialog, Ui_R_Source): def __init__(self, items=None): super(Remove_Source , self).__init__() self.ui = Ui_R_Source() self.ui.setupUi(self) self.setWindowTitle("Remove Source Folders") self.items = items or [] for i in range(self.items.count()): self.ui.keep_window.addItem(self.items(i)) self.exec()
The list is inaccessible within the class. Anyone know what I'm doing wrong? Thanks
Lifetime Qt Championwrote on 1 Jun 2017, 11:29 last edited by jsulm 6 Jan 2017, 11:29@robbiecooper In your code there is even more wrong:
self.items.count()
does not do what you think it does and will cause you app to crash (count() counts how often an element, which is passed as parameter, occurs in the list).
It should belen(self.items)
-
wrote on 1 Jun 2017, 13:07 last edited by
Thanks @jsulm!! That sorted it!
2/4