Search code examples
pythonpyqtpyqt4

How to close QDialog when QMainWindow is closed


I'm using PyQt and I have a QMainWindow that spawns a QDialog window after a click signal. What I want is this QDialog to simply disappear after I close QMainWindow. I can't make much of the documentations and the c++ versions of this problem. Below is code:

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys

class App:

    def mainGui(self):
        self.mainWin = QMainWindow()
        self.mainWin.setGeometry(200,200,500,432)
        self.mainWin.show()
        mainMenu = self.mainWin.menuBar()
        mainMenu.setNativeMenuBar(False)
        aboutMenu = mainMenu.addMenu('A&bout')
        helpButton = QAction(QIcon(),'Help',self.mainWin)
        helpButton.setShortcut('F4')
        helpButton.triggered.connect(self.helpPopup)
        aboutMenu.addAction(helpButton)

    def helpPopup(self):
        self.popup = QDialog()
        self.popup.setWindowTitle('Help')
        self.popup.setGeometry(800,200,300,500)
        self.popup.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    a = App()
    a.mainGui()
    sys.exit(app.exec_())

Solution

  • In Qt, and therefore also in PyQt, if the parent dies the children too, so only pass as a parent of the QDialog to self.mainWin:

    self.popup = QDialog(self.mainWin)