Pythonian
Pythonian

Reputation: 41

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

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()

Upvotes: 3

Views: 18367

Answers (2)

Pythonian
Pythonian

Reputation: 41

I found a way. I used self.show() instead of dialog.show() and self.mdiAreainstead of window.mdiArea.

Now I close the window and show it again with the widgets I want. I want to find a way to just "refresh" the window. But this is subject for another topic.
Thanks a lot guys.

Upvotes: 1

Olivier Giniaux
Olivier Giniaux

Reputation: 950

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

Upvotes: 3

Related Questions