What is the correct way to make my PyQt application quit when killed from the console (Ctrl-C)?
Currently (I have done nothing special to handle unix signals), my PyQt application ignores SIGINT (Ctrl+C). I want it to behave nicely and quit when it is killed. How should I do that?
That means Python cannot handle signals while the Qt event loop is running. Only when the Python interpreter run (when the QApplication quits, or when a Python function is called from Qt) the signal handler will be called.
A solution is to use a QTimer to let the interpreter run from time to time.
Note that, in the code below, if there are no open windows, the application will quit after the message box regardless of the user’s choice because QApplication.quitOnLastWindowClosed() == True. This behaviour can be changed.
Another possible solution, as pointed by LinearOrbit, is
signal.signal(signal.SIGINT, signal.SIG_DFL), but it doesn’t allow custom handlers.