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. Proper way of closing an application
QtWS25 Last Chance

Proper way of closing an application

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 4.6k Views
  • 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.
  • U Offline
    U Offline
    unzu
    wrote on 25 Jan 2021, 08:56 last edited by
    #1

    Hello,

    I'm writing a small program to communicate with Bluetooth devices.

    So far it is working fine and I am trying to fix some minor details:

    I want to delay the closing of the app to give it time to disconnect from all devices:

    1. Right now I am overriding the internal onClose(QCloseEvent *event) function to event->ignore() the close-event and handle my own code.

    2. So this part is working fine, right now I am having an issue to call the close-event manually as soon as my code has finished.

    3. I tried QApplication::quit(), which stopped my program and closed all windows, however I feel like there is something wrong, because it leaves an icon of the app in the system-tray.

    4. That definitely doesn't happen when I use the proper event->accept() in the onClose(*) function.

    Any hints on how to delay and close the app manually are very appreciated!

    Thanks

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Cobra91151
      wrote on 25 Jan 2021, 10:01 last edited by Cobra91151
      #2

      Hello!

      You do not have to override the onClose event. I suggest you to to read this doc (https://doc.qt.io/qt-5/qapplication.html):

      We recommend that you connect clean-up code to the aboutToQuit() signal, instead of putting it in your application's main() function. This is because, on some platforms the QApplication::exec() call may not return. For example, on the Windows platform, when the user logs off, the system terminates the process after Qt closes all top-level windows. Hence, there is no guarantee that the application will have time to exit its event loop and execute code at the end of the main() function, after the QApplication::exec() call.

      Example:

      connect(QApplication::instance(), &QApplication::aboutToQuit, this, [this]() {
          //Your clean-up code
      });
      

      So, you have to use aboutToQuit signal (https://doc.qt.io/qt-5/qcoreapplication.html#aboutToQuit) to clean up your app resources before exiting. As for the app icon (QSystemTrayIcon) in the tray, you must deal with it separately. You can use the hide method (https://doc.qt.io/qt-5/qsystemtrayicon.html#hide) when aboutToQuit signal occurs. Also, keep in mind if QSystemTrayIcon is initialized as a pointer, you should call deleteLater() method after hide there as well. Happy coding!

      J 1 Reply Last reply 25 Jan 2021, 10:10
      3
      • C Cobra91151
        25 Jan 2021, 10:01

        Hello!

        You do not have to override the onClose event. I suggest you to to read this doc (https://doc.qt.io/qt-5/qapplication.html):

        We recommend that you connect clean-up code to the aboutToQuit() signal, instead of putting it in your application's main() function. This is because, on some platforms the QApplication::exec() call may not return. For example, on the Windows platform, when the user logs off, the system terminates the process after Qt closes all top-level windows. Hence, there is no guarantee that the application will have time to exit its event loop and execute code at the end of the main() function, after the QApplication::exec() call.

        Example:

        connect(QApplication::instance(), &QApplication::aboutToQuit, this, [this]() {
            //Your clean-up code
        });
        

        So, you have to use aboutToQuit signal (https://doc.qt.io/qt-5/qcoreapplication.html#aboutToQuit) to clean up your app resources before exiting. As for the app icon (QSystemTrayIcon) in the tray, you must deal with it separately. You can use the hide method (https://doc.qt.io/qt-5/qsystemtrayicon.html#hide) when aboutToQuit signal occurs. Also, keep in mind if QSystemTrayIcon is initialized as a pointer, you should call deleteLater() method after hide there as well. Happy coding!

        J Offline
        J Offline
        JonB
        wrote on 25 Jan 2021, 10:10 last edited by
        #3

        @Cobra91151
        The problem here is that aboutToQuit() will return when the slot code returns. The way @unzu describes his situation with

        I want to delay the closing of the app to give it time to disconnect from all devices:

        makes me think he needs to give time for the clean up to complete, and he wants to do this asynchronously, not blocking. Is that the case?

        U 1 Reply Last reply 25 Jan 2021, 12:24
        1
        • J JonB
          25 Jan 2021, 10:10

          @Cobra91151
          The problem here is that aboutToQuit() will return when the slot code returns. The way @unzu describes his situation with

          I want to delay the closing of the app to give it time to disconnect from all devices:

          makes me think he needs to give time for the clean up to complete, and he wants to do this asynchronously, not blocking. Is that the case?

          U Offline
          U Offline
          unzu
          wrote on 25 Jan 2021, 12:24 last edited by
          #4

          @Cobra91151 said in Proper way of closing an application:

          Also, keep in mind if QSystemTrayIcon is initialized as a pointer, you should call deleteLater() method after hide there as well.

          I never coded that icon, its just a black slot in the tray.
          I think its generated automatically by the MainWindow.

          @JonB said in Proper way of closing an application:

          makes me think he needs to give time for the clean up to complete, and he wants to do this asynchronously, not blocking. Is that the case?

          Yes, that is the case.
          If possible I need to ignore the QCloseEvent, do my stuff, and fire another QCloseEvent.
          I just don't know how to fire that, its generated by clicking the red X (close) button of the MainWindow.

          I hoped there would be a way to generate that event via code and let Qt handle the hidden stuff.

          J 1 Reply Last reply 25 Jan 2021, 12:25
          0
          • U unzu
            25 Jan 2021, 12:24

            @Cobra91151 said in Proper way of closing an application:

            Also, keep in mind if QSystemTrayIcon is initialized as a pointer, you should call deleteLater() method after hide there as well.

            I never coded that icon, its just a black slot in the tray.
            I think its generated automatically by the MainWindow.

            @JonB said in Proper way of closing an application:

            makes me think he needs to give time for the clean up to complete, and he wants to do this asynchronously, not blocking. Is that the case?

            Yes, that is the case.
            If possible I need to ignore the QCloseEvent, do my stuff, and fire another QCloseEvent.
            I just don't know how to fire that, its generated by clicking the red X (close) button of the MainWindow.

            I hoped there would be a way to generate that event via code and let Qt handle the hidden stuff.

            J Offline
            J Offline
            JonB
            wrote on 25 Jan 2021, 12:25 last edited by
            #5

            @unzu
            You don't "generate the event via code", you just call close() on the object.

            U 1 Reply Last reply 30 Jan 2021, 15:42
            3
            • J JonB
              25 Jan 2021, 12:25

              @unzu
              You don't "generate the event via code", you just call close() on the object.

              U Offline
              U Offline
              unzu
              wrote on 30 Jan 2021, 15:42 last edited by
              #6

              @JonB said in Proper way of closing an application:

              You don't "generate the event via code", you just call close() on the object.

              Thanks!

              I am calling close() now, I like that quite a bit more than the QApplication::quit().

              1 Reply Last reply
              0

              1/6

              25 Jan 2021, 08:56

              • Login

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