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.
source share