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.
Asked
Active
Viewed 5,188 times
5

ManuelSchneid3r
- 15,850
- 12
- 65
- 103
-
What you want is to trap the SIGTERM signal in your process and handle it accordingly. More info [here](http://stackoverflow.com/questions/17942034/simple-linux-signal-handling). – Mohamad Elghawi Oct 12 '15 at 15:13
-
http://stackoverflow.com/a/975736/1122645 – UmNyobe Oct 12 '15 at 16:14
-
Thank you, these links contained the solution. – ManuelSchneid3r Oct 13 '15 at 09:45
1 Answers
5
There is a minimal set of functions a unix signal handler is allowed to call. They are called async-signal-safe functions. Calling everything else, including every Qt function, results in undefined behavior.
Still there is a way to handle unix signals in Qt. The way uses the self-pipe-trick and is described in the Qt docs article "Calling Qt Functions From Unix Signal Handlers".
Basically you open a pipe and whenever you get a signal you ::write(...)
(this is a async-signal-safe function) to the pipe. On the other end you listen to the pipe using a QSocketNotifier. For the implementation details check the Qt article mentioned above.

ManuelSchneid3r
- 15,850
- 12
- 65
- 103