lloydyu24
lloydyu24

Reputation: 839

Attribute Error: object has no attribute error -- subclassing GUI PyQt4

I added a new modal Dialog which will ask for a password whenever the user wants to delete rows. I created the GUI with Qt Designer. I can't call the inventoryTable in my GUI.

Everytime I call secureDelete function, an Attribute Error arises saying "'MainWindow_ui' object has no attribute 'inventoryTable'".

Can anyone tell me how I add other UIs properly and also be able to avoid this attribute errors in the future?

class Main(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.db = Database()
        self.model = Model(self)
        self.ui = MainWindow_ui()
        self.ui.setupUi(self)

        self.ui.removeItem.clicked.connect(lambda: self.start_Secure())
        self.ui.removeItem.setShortcut("Del")

    def start_Secure(self):
        self.accessForm = confirmDialog(self)
        self.accessForm.show()

class confirmDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(confirmDialog, self).__init__(parent)
        self.model = Model()
        self.access_ui = Ui_Access()
        self.access_ui.setupUi(self)
        self.ui = MainWindow_ui()

        self.access_ui.username.returnPressed.connect(self.secureDelete)
        self.access_ui.password.returnPressed.connect(self.secureDelete)
        self.access_ui.confirmButton.clicked.connect(self.secureDelete)

    def secureDelete(self):
        members = {'user': 'password'}
        username = self.access_ui.username.text()
        password = self.access_ui.password.text()

        if username in members:
            enteredPass = members.get(username)
            indices = self.ui.inventoryTable.selectionModel().selectedRows()
            if password == enteredPass:
                for index in sorted(indices):
                    self.model.removeRow(index.row())
            else:
                self.model.revertRow(indices)

Upvotes: 0

Views: 276

Answers (1)

eyllanesc
eyllanesc

Reputation: 243945

The classes generated by Qt Designer provide the setupUi() method to create the design elements so that in your case you have not called it, and consequently never created the inventoryTable, but even passing it using that function would not achieve your goal since you would be creating a new instance.

In your particular case as you have passed to self as parent of confirmDialog:

self.accessForm = confirmDialog(self)

Then you can access Main through the parent() method in your case do the following:

class confirmDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        [...]
        self.access_ui.setupUi(self)
        # self.ui = MainWindow_ui()
        [...]

    def secureDelete(self):
        [...]

        if username in members:
            enteredPass = members.get(username)
            indices = self.parent().ui.inventoryTable.selectionModel().selectedRows()
            [...]

Upvotes: 1

Related Questions