How to put a child window inside a main window(PyQt)

Pythonian picture Pythonian · May 27, 2015 · Viewed 13.4k times · Source

I looked here in stackoverflow and google for some days for something like my case, but all the examples I found did not work.

What I want is to have my parent window with the menu, and then call other child windows from that menu and execute/show them inside the parent window.

I tried put a widget in the parent window and call the child window inside of it, use MDIArea, but nothing worked.

Obs.: My screen files are generated from Qt designer, and I'm making separated classes to manipulate the widgets, pushbuttons, etc to keep everything more organized.

I created MdiArea in my main window using QtDesigner and them in a class triggered by clicking a menu I call the subwindow (a widget created with QtDesigner too) inside the MdiArea.

from resources.SubWindowQtDes import Ui_SubWindow
from resources.MainWindowQTDes import Ui_MainWindow
class cadastraAluno(Ui_SubWindow,Ui_MainWindow):
    def __init__(self, parent=None):
        super(cadastraAluno, self).__init__(parent = None)
        dialog = Ui_SubWindow()
        window = Ui_MainWindow()
        mdi = window.mdiArea
        mdi.addSubWindow(dialog, flags = 0)
        dialog.show()

Answer

Olivier Giniaux picture Olivier Giniaux · May 28, 2015

Here what I usually do for child windows :

class subwindow(QtGui.QWidget):
    def createWindow(self,WindowWidth,WindowHeight):
       parent=None
       super(subwindow,self).__init__(parent)
       selt.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
       self.resize(WindowWidth,WindowHeight)

class mainwindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
       [...]

    def createsASubwindow(self):
       self.mySubwindow=subwindow()
       self.mySubwindow.createWindow(500,400)
       #make pyqt items here for your subwindow
       #for example self.mySubwindow.button=QtGui.QPushButton(self.mySubwindow)

       self.mySubwindow.show()

This way you have a subwindow that always stays on top of main window and which can only be instantiated once.

I hope it helped