1

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

1 Answer 1

1

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_())
Sign up to request clarification or add additional context in comments.

3 Comments

@ eyllanesc I'm really sorry, I'm searching for this since last night and unable to resolve it and no ide how to resolve so I know u are an active helper so I tagged u really sorry for that
@ eyllanesc sir "Raju" is my friend and it's his acc his also working on a project on pyqt so he asked me that how to solve I don't know exactly how to do so I asked him to post here.As there was no response I tagged u here as this post a bit relevant to that
@ eyllanesc sir, I will work on improving them sorry for that.can u please hemp me solve that query which i tagged please sir thank you sir

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.