Determine reason why application was closed
-
How can I determine in which way my GUI application is being closed?
- Via UI (Keyboard shortcut, close button, via context menu on taskbar icon)
- Via task manager
- Via system shutdown
I need to distinguish these cases and execute some code before closing. My expectation was that the QCloseEvent would provide some insight, but it does not.
Any suggestions?
-
Hi,
- This one being "normal" operation, you can execute your code after
app.exec()
returned. - Don't know
- Session management
- This one being "normal" operation, you can execute your code after
-
@SGaist
I'm not sure I understand how you would actually distinguish between a regular, UI-triggered close vs. a system shutdown:- I would expect that the code after app.exec() runs in both cases, unless my process hangs and gets killed by the OS
- The session manager does not seem to provide any information about why it asks me to commit data (which after a brief look at the docs seems to be the appropriate entry point for my code to do something).
-
@Asperamanca
If it's via Task Manager, so far as I know your program just gets "killed" with no chance for you to do anything, no? (Don't know whether the low-level Windows/Csignal()
call gets called, like it would for a "soft"kill
[notkill -9
] under Linux, I kinda think it does not under Windows, but you could try.....)EDIT: A random post from the web states:
No, there is no way to know when YOUR PROCESS is being terminated if task manager uses TerminateProcess() API (this is done in case of "force kill option").
See also the thread on this forum https://forum.qt.io/topic/89525/slot-exception-being-thrown-on-exit-of-qt-app-with-pkill
-
@JonB said in Determine reason why application was closed:
@Asperamanca
If it's via Task Manager, so far as I know your program just gets "killed" with no chance for you to do anything, no? (Don't know whether the low-level Windows/Csignal()
call gets called, like it would for a "soft"kill
[notkill -9
] under Linux, I kinda think it does not under Windows, but you could try.....)EDIT: A random post from the web states:
No, there is no way to know when YOUR PROCESS is being terminated if task manager uses TerminateProcess() API (this is done in case of "force kill option").
See also the thread on this forum https://forum.qt.io/topic/89525/slot-exception-being-thrown-on-exit-of-qt-app-with-pkill
It depends on where in the task manager you choose to close the program.
- In the "application" tab, the program is at first closed normally. Only if the program hangs and the task manager reaches a timeout, and the user chooses to 'kill' the application is the process killed.
- In the "processes" tab. There, the program is actually killed without warning.
-
@Asperamanca
Oh, I have never done anything from Application tab, always from Process tab.So what does "the program is at first closed normally" mean, if that's what you want to trap? A
WM_QUIT
gets posted to its Windows event loop, or what? Did anything in https://forum.qt.io/topic/89525/slot-exception-being-thrown-on-exit-of-qt-app-with-pkill help for that? -
@JonB said in Determine reason why application was closed:
@Asperamanca
Oh, I have never done anything from Application tab, always from Process tab.So what does "the program is at first closed normally" mean, if that's what you want to trap? A
WM_QUIT
gets posted to its Windows event loop, or what? Did anything in https://forum.qt.io/topic/89525/slot-exception-being-thrown-on-exit-of-qt-app-with-pkill help for that?Since the Application tab is tied to a visible window, a WM_CLOSE is sent to that window, followed by WM_DESTROY and WM_NCDESTROY.
I just traced that for a random application using Spy++.