How to access attribut from one class to another class in PyQt5 / PySide2

Viewed 54

I cannot get access to attribut self.horizontalLayout_Base in class Ui_MainWindow where I want add new widgets.

There is :

  1. Main class MainWindow which inherits class Ui_MainWindow.
  2. Class Ui_MainWindow creates layout.
  3. Class Dialog provides additional information and confirmation or cancellation.

Main class MainWindow calls class Dialog. Class Dialog is queried and then the method tableWidget in class MainWindow is called. But there is not access to attribut self.horizontalLayout_Base which was creating by class Ui_MainWindow.

Is there any way to get access?

Main class MainWindow

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    
    def __init__(self, *args, obj=None, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        
        ...
        
        self.setupUi(self)
        
        ...
        
        
    def tableWidget(self):
        if len(list) > 0:
            row = len(list)
            clmn = len(list[0]) 
            self.tableWidget = QTableWidget(row, clmn + 1)
        else:
            return
        self.vbox = QVBoxLayout()

        ...

        Ui_MainWindow.gridLayout_Base.addLayout(self.vbox) # Here I want to get access

Layout class Ui_MainWindow

class Ui_MainWindow(object):

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1110, 772)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.centralwidget.sizePolicy().hasHeightForWidth())
        self.centralwidget.setSizePolicy(sizePolicy)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")

        self.horizontalLayout_Base = QtWidgets.QHBoxLayout() # This is target

        ...

Class Dialog

class Dialog(QDialog):

    NumGridRows = 3
    NumButtons = 4

    def __init__(self, souradnice):
        super(Dialog, self).__init__()
        ...

    def buttonAccepted(self):
        ...

        MainWindow.tableWidget(self)
        self.close()

1 Answers

It seems like you're confusing what classes, instances and their methods are (for this, I strongly suggest you to do some research, as they are fundamental aspects of OOP and cannot be ignored).

In tableWidget you cannot access gridLayout_Base from Ui_MainWindow, because that's a class. Since you're both inheriting from QMainWindow and Ui_MainWindow, and you're calling setupUi(self) that means that all attributes of the ui are actually created as attribute members of the instance, so you can access the layout just by using self.gridLayout_Base.
Then, if you're adding a layout, you must use addLayout(), not addWidget() (which is for widgets only).

    def tableWidget(self):
        # ...
        self.gridLayout_Base.addLayout(self.vbox)

Then, I don't know how you're creating and opening the dialog, but you certainly cannot call MainWindow.tableWidget(self): that would result in calling that method with the dialog instance as first argument (self), so you won't be adding anything to the main window.

A better and safer way (which is also the commonly accepted practice) is to show the dialog as modal by calling its exec(), and then react to it according to the result. In order to correcty achieve this, you should not call self.close() on the dialog (which results in rejecting it) but self.accept().

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    # ...
    def someFunctionToShowDialog(self):
        dialog = Dialog(self)
        if dialog.exec_():
            parameters = dialog.getValues()
            self.tableWidget()
            self.doSomethingWithParameters(parameters)


class Dialog(QDialog):
    # ...
    def buttonAccepted(self):
        self.accept()

    def getValues(self):
        return self.someValue, self.anotherValue
Related