Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Determine reason why application was closed
Forum Updated to NodeBB v4.3 + New Features

Determine reason why application was closed

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.1k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      1. This one being "normal" operation, you can execute your code after app.exec() returned.
      2. Don't know
      3. Session management

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      3
      • SGaistS SGaist

        Hi,

        1. This one being "normal" operation, you can execute your code after app.exec() returned.
        2. Don't know
        3. Session management
        A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #3

        @SGaist
        I'm not sure I understand how you would actually distinguish between a regular, UI-triggered close vs. a system shutdown:

        1. I would expect that the code after app.exec() runs in both cases, unless my process hangs and gets killed by the OS
        2. 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).
        JonBJ 1 Reply Last reply
        0
        • A Asperamanca

          @SGaist
          I'm not sure I understand how you would actually distinguish between a regular, UI-triggered close vs. a system shutdown:

          1. I would expect that the code after app.exec() runs in both cases, unless my process hangs and gets killed by the OS
          2. 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).
          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @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/C signal() call gets called, like it would for a "soft" kill [not kill -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

          A 1 Reply Last reply
          1
          • JonBJ JonB

            @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/C signal() call gets called, like it would for a "soft" kill [not kill -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

            A Offline
            A Offline
            Asperamanca
            wrote on last edited by
            #5

            @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/C signal() call gets called, like it would for a "soft" kill [not kill -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.
            JonBJ 1 Reply Last reply
            0
            • A Asperamanca

              @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/C signal() call gets called, like it would for a "soft" kill [not kill -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.
              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by JonB
              #6

              @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?

              A 1 Reply Last reply
              0
              • JonBJ JonB

                @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?

                A Offline
                A Offline
                Asperamanca
                wrote on last edited by
                #7

                @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++.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved