PDA

View Full Version : Graceful termination of Qt application by unix signal



manuelschneid3r
12th October 2015, 17:22
I have problems saving settings in my application. This is done in the destructors of the relevant objects. It is a launcher and a termination by shutdown is a standard case. The only way the application actually saves the setting is by manual closing it or session shutdown (on cinnamon at least, I guess this just closes all windows). Even sudo reboot prevents the Qt application from unwinding the objects on the stack. Terminating by killall -s <signal> <app> has the same effect for SIGINT, SIGKILL and SIGTERM. How can I force my qt app to gracefully terminate at on SIGTERM? aboutToQuit is not emitted either.

anda_skoa
12th October 2015, 18:51
You probably need a signal handler that quits your application on SIGTERM.
See "man signal"

Cheers,
_

manuelschneid3r
19th October 2015, 15:33
Thank you, sir.

yeye_olive
19th October 2015, 17:42
Oh, and remember that you cannot do much in a signal handler, because it can be called asynchronously, for instance in the middle of some code manipulating a crucial data structure. A popular solution called the "self-pipe trick" (cf Google) consists in having the signal handler write a byte to a pipe, and the main event loop wait for read events on the other end of the pipe. In Qt, use QSocketNotifier to monitor those read events. If you are using Linux, consider using eventfd() instead of a pipe.

anda_skoa
24th October 2015, 17:03
A popular solution called the "self-pipe trick"
In Qt it should also work to post an event to an object or using QMetaObject::invokeMethod() with Qt::QueuedConnection.

E.g.


QMetaObject::invokeMethod(qApp, "quit", Qt::QueuedConnection);


Cheers,
_