[PyQt] Intercept close button and force application to Quit

Jonathan Harper jon at white-walls.net
Thu Dec 23 18:04:19 GMT 2010


>I have an application with several threads, one of which is a PyQT GUI,
>when I click on the close button, the PyQT GUI closes but the rest of
>the application keeps running, I would like to be able to intercept the
>close button event and force the whole application to quit.
>Does anybody know how I might do this?

Either: 1) subclass QApplication and re-implement exit() or 2) subclass
QMainWindow and re-implement the QMainWindow close() method--the latter
assumes you're using a QMainWindow or subclassed QMainWindow as the
top-level widget. If you're using a QDialog or something else, the relevant
methods would have to be fussed with. I usually go with subclassing
QApplication, since it will catch more than just a safe close() call to the
top-level widget (e.g. a qFatal error in one of the threads). There are
times when it would be necessary to subclass a child widget's close()
method, such as if that widget (e.g. a modal QDialog or an MdiSubWindow)
launched some helper threads. Otherwise, the QApplication.exit() route is
probably better.

-- Jon Harper



More information about the PyQt mailing list