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. How to detect Windows shutdown or logoff and force quit to cleanly exit Qt qpplication
Forum Updated to NodeBB v4.3 + New Features

How to detect Windows shutdown or logoff and force quit to cleanly exit Qt qpplication

Scheduled Pinned Locked Moved General and Desktop
7 Posts 6 Posters 8.9k Views 1 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
    adnan
    wrote on last edited by
    #1

    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

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

      Hi,

      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

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        adnan
        wrote on last edited by
        #3

        AbouTtoQuit() works for shutdown and logoff, I needed something to catch kill from Task manager

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Asperamanca
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            according to "this post":http://stackoverflow.com/a/482737 it isn't possible in windows.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mehrdadsilver
              wrote on last edited by
              #6

              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.

              Mehrdad Abdolghafari, Be silver

              1 Reply Last reply
              0
              • T3STYT Offline
                T3STYT Offline
                T3STY
                wrote on last edited by
                #7

                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:

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

                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.

                Good luck!

                EDIT
                Still related to the SCH, have a look at this too:
                http://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/45646537-1b43-4420-ada3-06583c9d40cb/prevent-system-shutdown-from-a-service?forum=powermanagement

                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