PyQt: How to hide QMainWindow

Make the first window a parent of the second window:

class Dialog_02(QtGui.QMainWindow):
    def __init__(self, parent):
        super(Dialog_02, self).__init__(parent)
        # ensure this window gets garbage-collected when closed
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
    ...    

    def closeAndReturn(self):
        self.close()
        self.parent().show()

class Dialog_01(QtGui.QMainWindow):
    ...

    def callAnotherQMainWindow(self):
        self.hide()
        self.dialog_02 = Dialog_02(self)
        self.dialog_02.show()

If you want the same dialog to be shown each time, do something like:

    def callAnotherQMainWindow(self):
        self.hide()
        if not hassattr(self, 'dialog_02'):
            self.dialog_02 = Dialog_02(self)
        self.dialog_02.show()

and hide() the child window, rather than closing it.

Leave a Comment