How to detect Windows shutdown or logoff and force quit to cleanly exit Qt qpplication
adnan last edited by
I am porting a Linux app to Windows written in Qt. The application needs to save some settings before closing. On Linux, we can do that by signal handlers for SIGTERM etc. How can I implement the same on Windows. I tried using aboutToQuit() but it is not invoked if I kill the application using task manager
I can't tell how you can catch a kill from the task manager however for the logoff/shutdown you can use the "QGuiApplication::commitDataRequest":http://qt-project.org/doc/qt-5.1/qtgui/qguiapplication.html#commitDataRequest signal. However it will only work starting Qt 5.2 (or if you're using the 4 series it's already good)
Hope it helps
adnan last edited by
AbouTtoQuit() works for shutdown and logoff, I needed something to catch kill from Task manager
Asperamanca last edited by
I do not believe there is any notification to the process itself when it is killed. There are ways to prevent an application from being killed (e.g. virus scanner services usually can't just be terminated that way), but I frankly don't know how to do that.
You could start a second "watcher" process that always knows the current settings, and can write them in case the regular application goes down without waving goodbye first.
according to "this post":http://stackoverflow.com/a/482737 it isn't possible in windows.
mehrdadsilver last edited by
Hi, if you are working in GUI Application ,you can use t with a auxiliary bool flag & closeEvent in main widget. closeEvent is emitted when user close widget. in this event you should switch flag to true. in destruction member check if it is not true, windows is shutting down.
T3STY last edited by
If you want to make your application a bit more os-specific, Windows provides the Service Control Handler. It registers an application as a service and with it you can intercept a few nice messages like SERVICE_CONTROL_SHUTDOWN. This message is actually sent by Windows when a service must be stopped, which doesn't necessarily mean that windows is shutting down (a user might manually stop the service). The good news anyway is that when Windows shuts down it sends this message to all running services. If a user doesn't terminate the application at runtime and doesn't terminate the service, then chances are you're likely going to only get the SERVICE_CONTROL_SHUTDOWN message when Windows is shutting down.
Here's the MSDN link, should you be interested in this:
Also, I was having a look at Windows's window messages WM_CLOSE and WM_QUIT; they seem to be sent to an application when it needs to quit or to be closed, but I didn't actually understood if these messages are sent during Windows's shutdown procedure or not. If you have some spare time you might want to create a test application for these messages and see if you can use them.
Still related to the SCH, have a look at this too: