0

I am quite new in pyqt design and I have a question.

I have a Main form. and a child form. I want the child form to be opened inside the parent form.

how can I do that in python?

I have written a code that just open the form. but it didnot open it inside the form?

Thanks

##this method creates an Form Main
class FrmMainForm (QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.uiMain = FrmMain.Ui_MainWindow()
        self.uiMain.setupUi(self)
        action = self.uiMain.menuManual_Mode.addAction(self.tr('ManualMode'))
        action.triggered.connect(self.handleNewWindow)

    def handleNewWindow(self):
       ex = Main(self.uiMain)
       ex.show()


class Main(QtGui.QWidget):
    white   =  "QWidget { background-color:#FFFFFF }"
    red     =  "QWidget { background-color:#AB0000}"
    green   =  "QWidget { background-color:#00C000}"

    def __init__(self,p):
       QtGui.QWidget.__init__(self)
       self.p = p
       self.initLogAnalyzerManuall()


    def initLogAnalyzerManuall(self):
        QtGui.QWidget.__init__(self)
        self.ui = Ui_Form()
        self.ui.setupUi(self,self.p)

1 Answer 1

2

The child window will be garbage-collected when the handleNewWindow method returns, and so it will never get the chance to be shown.

You need to keep a reference to the child window, but the way you go about it depends on what you are trying to achieve. Do you literally want a new window to be opened every time the button is clicked? If so, then do this:

    def handleNewWindow(self):
       ex = Main(self)
       ex.show()

class Main(QtGui.QWidget):
    ...
    def __init__(self, parent):
        QtGui.QWidget.__init__(self, parent)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.ui = Ui_Form()
        self.ui.setupUi(self)

On the other hand, if you want the same child window to be opened every time, then do this:

class FrmMainForm (QtGui.QWidget):
    def __init__(self):
        ...
        self._child_window = None

    def handleNewWindow(self):
       if self._child_window is None:
           self._child_window = Main(self)
       self._child_window.show()

class Main(QtGui.QWidget):
    ...
    def __init__(self, parent):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)

Note that in both cases, you can access the main window from the child window with self.parent().

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.