Lehue
Lehue

Reputation: 435

How to find out if second window is closed

I have an application that uses another application from someone else halfway through (both applications use .ui files). Therefore I create the second application in SecondWindow and hide MainWindow. Now I would like to show MainWindow again after SecondWindow is closed. I found the solution in the answer works, but now the background of SecondWindow is wrong because it uses the background of MainWindow. Is there a way to find out if SecondWindow is closed in the class from MainWindow without making MainWindow a parent of SecondWindow or to prevent the background changes caused by the parenthood?

My current code looks somewhat like this:

## Define main window class from template
path = os.path.dirname(os.path.abspath(__file__))
uiFile = os.path.join(path, 'test.ui')
Ui_MainWindow, QtBaseClass = uic.loadUiType(uiFile)

uiFile2 = os.path.join(path, 'monitor.ui')
WindowTemplate, SecondWindowClass = pg.Qt.loadUiType(uiFile2)

class SecondWindow(SecondWindowClass):

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

    def __init__(self, parent):
        super(SecondWindow, self).__init__(parent)
        # ensure this window gets garbage-collected when closed
        self.setWindowTitle('pyqtgraph example: Qt Designer')
        self.ui = WindowTemplate()
        self.ui.setupUi(self)
        self.show()

class MainWindow(QtGui.QMainWindow, Ui_MainWindow):

     def showSecond(self):
        self.second.show()
        self.hide()

     def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.ui=uic.loadUi(uiFile, self)
        self.setupUi(self)
        self.show()
        self.second = SecondWindow(self)
        self.second.hide()
        self.ui.end_button.clicked.connect(lambda x:self.showSecond())

win = MainWindow()
if __name__ == '__main__':
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
       QtGui.QApplication.instance().exec_()

Upvotes: 1

Views: 140

Answers (1)

ekhumoro
ekhumoro

Reputation: 120578

It's not actually necessary for the second window to be a child of the first.

So you should be able to do something like this:

class SecondWindow(SecondWindowClass):    
    def closeThis(self):
        self.close()
        self.first.show()

    def __init__(self, first):
        super(SecondWindow, self).__init__()
        self.first = first
        ...

class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
     def showSecond(self):
        self.second.show()
        self.hide()

     def __init__(self):
        ...
        self.second = SecondWindow(self)
        self.second.hide()

Upvotes: 1

Related Questions