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 process remaining events before shutdown
Forum Updated to NodeBB v4.3 + New Features

How process remaining events before shutdown

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 639 Views 2 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.
  • ocgltdO Offline
    ocgltdO Offline
    ocgltd
    wrote on last edited by
    #1

    My simple(ish) app outputs information using qDebug statements in destructors. I notice that when my app shuts down via exit(0), those qDebug statements don't run - or at least I don't see the output.

    Is there a statement I can place as the last line of my main.cpp to perform any remaining outputs, handle any remaining events in the event loop, etc. ?

    Christian EhrlicherC 1 Reply Last reply
    0
    • ocgltdO ocgltd

      @Christian-Ehrlicher That worked as expected. I checked the quit docs and it's not clear how/when I should use this.

      Is this a replacement for exit() only? Should this be the last line of every Qt program? What's the right way to use this method?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #7

      @ocgltd
      Basically once you are inside QCoreApplication::exec() that is when you should always use QCoreApplication::quit(). This does not actually exit the application, rather it exits the a.exec() you have. Code then executes anything after that in your call, which is usually nothing, as in your return a.exec(); being the final statement in your main() function.

      Using QCoreApplication::quit() (or more rarely QCoreApplication::exit()) exits the Qt application event loop "cleanly", and allows proper clean up. Using the non-Qt system exit() directly exits the C++ application, as it would whether Qt or not, and it not recommended for Qt applications.

      ocgltdO 1 Reply Last reply
      2
      • ocgltdO ocgltd

        My simple(ish) app outputs information using qDebug statements in destructors. I notice that when my app shuts down via exit(0), those qDebug statements don't run - or at least I don't see the output.

        Is there a statement I can place as the last line of my main.cpp to perform any remaining outputs, handle any remaining events in the event loop, etc. ?

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @ocgltd Properly clean up and delete your objects before you leave main()

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        ocgltdO 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          @ocgltd Properly clean up and delete your objects before you leave main()

          ocgltdO Offline
          ocgltdO Offline
          ocgltd
          wrote on last edited by
          #3

          @Christian-Ehrlicher My class is created on the stack, so I don't think there is more I can do to clean up. I tried putting my class creation inside another nested block as per below, but I still don't see the qDebug messages upon exit.

          int main(int argc, char *argv[])
          {
              QCoreApplication a(argc, argv);
              {
                  MyClass myclass;
                  QTimer::singleShot(20000, []() {
                      exit(0);
                  } );
                  return a.exec();
              }
          }
          
          Christian EhrlicherC 1 Reply Last reply
          0
          • ocgltdO ocgltd

            @Christian-Ehrlicher My class is created on the stack, so I don't think there is more I can do to clean up. I tried putting my class creation inside another nested block as per below, but I still don't see the qDebug messages upon exit.

            int main(int argc, char *argv[])
            {
                QCoreApplication a(argc, argv);
                {
                    MyClass myclass;
                    QTimer::singleShot(20000, []() {
                        exit(0);
                    } );
                    return a.exec();
                }
            }
            
            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            You call the systems exit() function so there is no cleanup done as described in the c++ documentation: https://en.cppreference.com/w/cpp/utility/program/exit

            Properly close the Qt app with QCoreApplication::quit()

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            ocgltdO 1 Reply Last reply
            3
            • Christian EhrlicherC Christian Ehrlicher

              You call the systems exit() function so there is no cleanup done as described in the c++ documentation: https://en.cppreference.com/w/cpp/utility/program/exit

              Properly close the Qt app with QCoreApplication::quit()

              ocgltdO Offline
              ocgltdO Offline
              ocgltd
              wrote on last edited by
              #5

              @Christian-Ehrlicher That worked as expected. I checked the quit docs and it's not clear how/when I should use this.

              Is this a replacement for exit() only? Should this be the last line of every Qt program? What's the right way to use this method?

              Christian EhrlicherC JonBJ 2 Replies Last reply
              0
              • ocgltdO ocgltd

                @Christian-Ehrlicher That worked as expected. I checked the quit docs and it's not clear how/when I should use this.

                Is this a replacement for exit() only? Should this be the last line of every Qt program? What's the right way to use this method?

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @ocgltd said in How process remaining events before shutdown:

                hat's the right way to use this method?

                You should use it when you want to properly shut down your application.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0
                • ocgltdO ocgltd

                  @Christian-Ehrlicher That worked as expected. I checked the quit docs and it's not clear how/when I should use this.

                  Is this a replacement for exit() only? Should this be the last line of every Qt program? What's the right way to use this method?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #7

                  @ocgltd
                  Basically once you are inside QCoreApplication::exec() that is when you should always use QCoreApplication::quit(). This does not actually exit the application, rather it exits the a.exec() you have. Code then executes anything after that in your call, which is usually nothing, as in your return a.exec(); being the final statement in your main() function.

                  Using QCoreApplication::quit() (or more rarely QCoreApplication::exit()) exits the Qt application event loop "cleanly", and allows proper clean up. Using the non-Qt system exit() directly exits the C++ application, as it would whether Qt or not, and it not recommended for Qt applications.

                  ocgltdO 1 Reply Last reply
                  2
                  • ocgltdO ocgltd has marked this topic as solved on
                  • JonBJ JonB

                    @ocgltd
                    Basically once you are inside QCoreApplication::exec() that is when you should always use QCoreApplication::quit(). This does not actually exit the application, rather it exits the a.exec() you have. Code then executes anything after that in your call, which is usually nothing, as in your return a.exec(); being the final statement in your main() function.

                    Using QCoreApplication::quit() (or more rarely QCoreApplication::exit()) exits the Qt application event loop "cleanly", and allows proper clean up. Using the non-Qt system exit() directly exits the C++ application, as it would whether Qt or not, and it not recommended for Qt applications.

                    ocgltdO Offline
                    ocgltdO Offline
                    ocgltd
                    wrote on last edited by
                    #8

                    @JonB Is there any benefit to callint quit() in a simple app which doesn't use an event loop?

                    JonBJ 1 Reply Last reply
                    0
                    • ocgltdO ocgltd

                      @JonB Is there any benefit to callint quit() in a simple app which doesn't use an event loop?

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #9

                      @ocgltd
                      No, it does nothing other than exiting the event loop, and if that is not running I don't know what it does/does nothing.

                      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