Search code examples
pythonpyqt5qmainwindow

Compose a PyQt5 UI from multiple classes


I have an UI designed in QT Designer. It is formed from three tabs:

1 - Test; 2 - Train Haar; 3 - Train Hog;

In every tabs I have some buttons, or some lines, or some widgets, but when I create the code to add functions to those buttons or widgets I want to have 3 classes for every tabs, one class only for first tab, one class only for second tab and so on.. And a fourth class who call all three classes and compose my UI. I do not know how to do this, I need every classes to inherit from QMainWindow? I need to setupUi in every class?

This is my current code:

import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from Qt_designer_UI import Ui_MainWindow

class Test(QtWidgets.QMainWindow):
    def __init__(self):
        super(Test, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

class Train_Haar(QtWidgets.QMainWindow):
    def __init__(self):
        super(Train_Haar, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)


class Train_HOG(QtWidgets.QMainWindow):
    def __init__(self):
        super(Train_HOG, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)


class Compose_UI(QtWidgets.QMainWindow):
    def __init__(self):
        super(Compose_UI, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        Test()
        Train_Haar()
        Train_HOG



if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = Compose_UI()
    window.show()
    sys.exit(app.exec_())

enter image description here


Solution

  • I need every classes to inherit from QMainWindow?

    No, it is not necessary since it is a widget that will be inside the tabs, and that widget can be of any type.

    I need to setupUi in every class?

    It is only obligatory to call setupUi if you use a generated class with the help of pyuic and Qt Designer, in your case the widgets that are in each tab are generated with Qt Designer? I see that I do not

    Keeping in mind that your .ui is

    enter image description here

    a possible solution is:

    import sys
    from PyQt5 import QtWidgets, QtCore, QtGui
    from Qt_designer_UI import Ui_MainWindow
    
    
    class Test(QtWidgets.QWidget):
        def __init__(self, parent=None):
            super(Test, self).__init__(parent)
            # for testing
            lay = QtWidgets.QVBoxLayout(self)
            lay.addWidget(QtWidgets.QPushButton("Test"))
    
    
    class Train_Haar(QtWidgets.QWidget):
        def __init__(self, parent=None):
            super(Train_Haar, self).__init__(parent)
            # for testing
            lay = QtWidgets.QVBoxLayout(self)
            lay.addWidget(QtWidgets.QPushButton("Train_Haar"))
    
    
    class Train_HOG(QtWidgets.QWidget):
        def __init__(self, parent=None):
            super(Train_HOG, self).__init__(parent)
            # for testing
            lay = QtWidgets.QVBoxLayout(self)
            lay.addWidget(QtWidgets.QPushButton("Train_HOG"))
    
    
    class Compose_UI(QtWidgets.QMainWindow):
        def __init__(self):
            super(Compose_UI, self).__init__()
            self.ui = Ui_MainWindow()
            self.ui.setupUi(self)
    
            test = Test()
            train_haar = Train_Haar()
            train_hog = Train_HOG()
    
            for w, tab in zip(
                (test, train_haar, train_hog), (self.ui.tab1, self.ui.tab2, self.ui.tab3)
            ):
                lay = QtWidgets.QVBoxLayout(tab)
                lay.addWidget(w)
    
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        window = Compose_UI()
        window.show()
        sys.exit(app.exec_())