How to detect shutdown or shutdown of Windows in Qt

I am porting a Linux application on Windows written in Qt. An application needs to save some settings before closing. On Linux, we can do this using signal handlers for SIGTERM, etc. How can I implement them on Windows.

+6
source share
4 answers

If you use the Qt event loop, you can catch the following signal:

void QCoreApplication :: aboutToQuit () [signal]

This signal is emitted when the application is about to leave the main event loop, for example. when the event loop level drops to zero. This can happen either after calling quit () from the application, or when users close the entire desktop session. A signal is especially useful if your application needs to clean up the last second. Please note that in this state, user interaction is not possible.

In addition, you can search for the following messages below if the above signal is not suitable for your use case:

WM_QUIT: http://msdn.microsoft.com/en-us/library/windows/desktop/ms632641(v=vs.85).aspx

WM_CLOSE: http://msdn.microsoft.com/en-us/library/windows/desktop/ms632617(v=vs.85).aspx

WM_QUERYENDSESSION: http://msdn.microsoft.com/en-US/library/windows/desktop/aa376890.aspx

WM_ENDSESSION: http://msdn.microsoft.com/en-US/library/windows/desktop/aa376889.aspx

+7
source

I think the other answers do not completely match. When you force an application to terminate, it is similar to SIGKILL on Unix. There is no way to handle this - save for time . What I mean by managing it ahead of time is that you save the settings every time they change. Of course, you can optimize this behavior, for example, save settings every few seconds if they are dirty, if you want to minimize the number of disk accesses (think about the energy consumption on mobile devices).

Many of these are handled by QSettings for you. As long as you use QSettings , you get reasonable behavior. If you save files yourself, use QSaveFile , since it deals with cleaning the file and approximating the replacement of the atomic file so that you do not lose settings if you kill (forced completion) occurs in the middle when you record.

The aboutToQuit signal emitted by QCoreApplication is what you want to respond to if you just want to do something when the application is prompted to exit. This is equivalent to processing a WM_QUIT message, or working with SIGTERM on Unix. Thus, doing this in a platform-specific way is pointless, since Qt is already doing this for you. Likewise, it makes no sense to process WM_CLOSE , since this is a message that only windows receive, and again Qt is already processing it for you.

You can also participate in the logout / shutdown process by setting QAbstractNativeEventFilter and process WM_ENDSESSION and WM_QUERYENDSESSION . This only makes sense if you want to know in advance that the application will be terminated. Unless you explicitly want to stop turning off / on, you don't need to worry about that.

+2
source

I think it would be better to handle the signal QApplication :: commitDataRequest (or QGuiApplication :: commitDataRequest in Qt5) instead of aboutToQuit. Just connect the signal to your function to save the settings.

Here are some related discussions: http://qt-project.org/forums/viewthread/33329

+1
source

I do not know Qt. If you can afford to be only Windows WM_QUERYENDSESSION and WM_ENDSESSION can be correct.

0
source

Source: https://habr.com/ru/post/955294/


All Articles