[PyQt] Handling of Ctrl-C/SIGINT in PyQt

Yuya Nishihara yuya at tcha.org
Mon Mar 16 13:27:22 GMT 2015


On Mon, 16 Mar 2015 14:05:47 +0100, Florian Bruhin wrote:
> It seems to be surprising for many people why Ctrl-C doesn't work in
> PyQt applications:
> 
>     http://stackoverflow.com/q/4938723
>     http://stackoverflow.com/q/5160577
>     https://www.mail-archive.com/pyqt@riverbankcomputing.com/msg13757.html
>     etc.
> 
> If I want to run custom code on Ctrl-C, it seems the only solution is
> the one presented at [1], i.e. running a QTimer which executes
> something like "lambda: None" all few milliseconds.
> 
> [1] http://stackoverflow.com/a/4939113
> 
> This is because Python only handles the signal after control has been
> passed to Python.

On Unix-like system, you can use signal.set_wakeup_fd() and QSocketNotifier.

 1. create rfd/wfd by os.pipe() or socket.socketpair()
 2. create QSocketNotifier that listens to rfd and runs Python interpreter
 3. signal.set_wakeup_fd() to write to wfd

The following patch works fine.

https://bitbucket.org/tortoisehg/thg/commits/550e1df5fbad

Regards,


More information about the PyQt mailing list