PyQt - how to add separate UI widget to QMainWindow PyQt - how to add separate UI widget to QMainWindow python python

PyQt - how to add separate UI widget to QMainWindow


Are you looking for something like this? I'm not really sure what your main_widget is

from PyQt4.QtCore import *from PyQt4.QtGui  import *import sysclass MyMainWindow(QMainWindow):    def __init__(self, parent=None):        super(MyMainWindow, self).__init__(parent)        self.form_widget = FormWidget(self)         self.setCentralWidget(self.form_widget) class FormWidget(QWidget):    def __init__(self, parent):                super(FormWidget, self).__init__(parent)        self.layout = QVBoxLayout(self)        self.button1 = QPushButton("Button 1")        self.layout.addWidget(self.button1)        self.button2 = QPushButton("Button 2")        self.layout.addWidget(self.button2)        self.setLayout(self.layout)app = QApplication([])foo = MyMainWindow()foo.show()sys.exit(app.exec_())


I would recommend using Qt Designer to create as much of the UI as possible.

You will find it much easier to experiment with layouts and so forth that way, and it will automatically keep most of the UI related stuff separate from the rest of your application logic. Do this for the main window, and also for any dialog boxes, however simple.

Then use pyuic4 to compile python modules from all the ui files, and put them all together in their own sub-package.

I would recommend using the -w flag when compiling ui files. This will generate a simple wrapper UI class that can be subclassed directly.

So your main window would end up looking something like this:

from ui.mainwindow import MainWindowUIclass MainWindow(MainWindowUI):    def __init__(self):        super(MainWindow, self).__init__()        # connect signals...        # do other setup stuff...

Note that all the widgets added in Qt Designer are now accessible directly as attributes of the MainWindow instance.

I would not worry about breaking your application up into smaller modules until later on in development. It may not turn out to be necessary - but if it does, it will become more obvious how to do this once the application starts to become more complex.

There are no hard and fast rules - every project is different.


import sysfrom PyQt4 import QtCore, QtGuiclass MainWindow(QtGui.QMainWindow):    def __init__(self, parent=None):        super(MainWindow, self).__init__(parent)        self.form_widget = FormWidget(self)        _widget = QtGui.QWidget()        _layout = QtGui.QVBoxLayout(_widget)        _layout.addWidget(self.form_widget)        self.setCentralWidget(_widget)class FormWidget(QtGui.QWidget):    def __init__(self, parent):        super(FormWidget, self).__init__(parent)        self.__controls()        self.__layout()    def __controls(self):        self.label = QtGui.QLabel("Name for backdrop")        self.txted = QtGui.QLineEdit()        self.lbled = QtGui.QLabel("Select a readNode")        self.cmbox = QtGui.QComboBox()    def __layout(self):        self.vbox = QtGui.QVBoxLayout()        self.hbox = QtGui.QHBoxLayout()        self.h2Box = QtGui.QHBoxLayout()        self.hbox.addWidget(self.label)        self.hbox.addWidget(self.txted)        self.h2Box.addWidget(self.lbled)        self.h2Box.addWidget(self.cmbox)        self.vbox.addLayout(self.hbox)        self.vbox.addLayout(self.h2Box)        self.setLayout(self.vbox)def main():    app = QtGui.QApplication(sys.argv)    win = MainWindow()    win.show()    app.exec_()if __name__ == '__main__':    sys.exit(main()) 

correct way!!!