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. A bug was found when calling qApp->quit()
Forum Updated to NodeBB v4.3 + New Features

A bug was found when calling qApp->quit()

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 5 Posters 921 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.
  • P Pleafles

    Thank you very much, but I do need modal dialog boxes in some places, and I also use some other methods to solve my problem, but I hope to solve this problem at the bottom, so I'll give you some suggestions

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #4

    @Pleafles said in A bug was found when calling qApp->quit():

    Thank you very much, but I do need modal dialog boxes in some places

    calling exec() on a dialog doesn't make it a modal one, calling setModal
    If you mean with modal, you want to remain in the function until the dialog is closed, that different and a sign of bad design. One can easily work around that.


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    P 1 Reply Last reply
    0
    • P Pleafles

      When a modal dialog box is running (QMessagebox::exec()), calling qApp->quit() cannot exit the application, but directly closes the modal dialog box. If qApp->quit() is replaced with exit(0), the function will not execute atexit() function (it will run normally) and directly close the program.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #5

      @Pleafles said in A bug was found when calling qApp->quit():

      When a modal dialog box is running (QMessagebox::exec()), calling qApp->quit() cannot exit the application, but directly closes the modal dialog box.

      This works correctly:

      #include <QApplication>
      #include <QDialog>
      #include <QMainWindow>
      #include <QTimer>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          QMainWindow window;
      
          QMetaObject::invokeMethod(&a, [&window] () -> void  {
              QDialog dialog(&window);
              QTimer::singleShot(5000, [] () -> void  {
                  qApp->quit();
              });
              dialog.exec();
          }, Qt::QueuedConnection);
      
          window.show();
          return a.exec();
      }
      

      so I'm not convinced.

      If qApp->quit() is replaced with exit(0), the function will not execute atexit() function (it will run normally) and directly close the program.

      If you terminate the application, it's going to be terminated. How is this strange?
      https://en.cppreference.com/w/cpp/utility/program/exit

      Read and abide by the Qt Code of Conduct

      P 1 Reply Last reply
      2
      • J.HilkJ J.Hilk

        @Pleafles said in A bug was found when calling qApp->quit():

        Thank you very much, but I do need modal dialog boxes in some places

        calling exec() on a dialog doesn't make it a modal one, calling setModal
        If you mean with modal, you want to remain in the function until the dialog is closed, that different and a sign of bad design. One can easily work around that.

        P Offline
        P Offline
        Pleafles
        wrote on last edited by
        #6

        @J-Hilk Thank you, but there will still be problems using setModal,for example:

        QDialog dialog(&w);
        QTimer::singleShot(5000, [] () -> void  {
            qApp->quit();
        });
        dialog.setModal(false);
        dialog.exec();
        

        It still doesn't work...

        1 Reply Last reply
        0
        • kshegunovK kshegunov

          @Pleafles said in A bug was found when calling qApp->quit():

          When a modal dialog box is running (QMessagebox::exec()), calling qApp->quit() cannot exit the application, but directly closes the modal dialog box.

          This works correctly:

          #include <QApplication>
          #include <QDialog>
          #include <QMainWindow>
          #include <QTimer>
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              QMainWindow window;
          
              QMetaObject::invokeMethod(&a, [&window] () -> void  {
                  QDialog dialog(&window);
                  QTimer::singleShot(5000, [] () -> void  {
                      qApp->quit();
                  });
                  dialog.exec();
              }, Qt::QueuedConnection);
          
              window.show();
              return a.exec();
          }
          

          so I'm not convinced.

          If qApp->quit() is replaced with exit(0), the function will not execute atexit() function (it will run normally) and directly close the program.

          If you terminate the application, it's going to be terminated. How is this strange?
          https://en.cppreference.com/w/cpp/utility/program/exit

          P Offline
          P Offline
          Pleafles
          wrote on last edited by
          #7

          @kshegunov Thank you very much, it works normally, but there will be problems with its use:

          ...
          QApplication a(argc, argv);
          QMainWindow window;
          window.show();

              QDialog dialog(&window);
              QTimer::singleShot(5000, [] () -> void  {
                  qApp->quit();
              });
              dialog.exec();
          
          return a.exec();
          

          It's strange why this doesn't work,and and it will crash...

          JKSHJ kshegunovK 2 Replies Last reply
          0
          • P Pleafles

            @kshegunov Thank you very much, it works normally, but there will be problems with its use:

            ...
            QApplication a(argc, argv);
            QMainWindow window;
            window.show();

                QDialog dialog(&window);
                QTimer::singleShot(5000, [] () -> void  {
                    qApp->quit();
                });
                dialog.exec();
            
            return a.exec();
            

            It's strange why this doesn't work,and and it will crash...

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by JKSH
            #8

            @Pleafles said in A bug was found when calling qApp->quit():

            It's strange why this doesn't work,and and it will crash...

            [EDIT: Sorry, this is wrong. Please get a stack trace, as @kshegunov suggested]

            Because your code will try to delete the dialog twice. See https://doc.qt.io/qt-5/objecttrees.html#construction-destruction-order-of-qobjects

            You must do one of the following:

            • Do not set a parent for your dialog, OR
            • Allocate the dialog on the heap (by calling new QDialog), not on the stack

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            kshegunovK 1 Reply Last reply
            0
            • JKSHJ JKSH

              @Pleafles said in A bug was found when calling qApp->quit():

              It's strange why this doesn't work,and and it will crash...

              [EDIT: Sorry, this is wrong. Please get a stack trace, as @kshegunov suggested]

              Because your code will try to delete the dialog twice. See https://doc.qt.io/qt-5/objecttrees.html#construction-destruction-order-of-qobjects

              You must do one of the following:

              • Do not set a parent for your dialog, OR
              • Allocate the dialog on the heap (by calling new QDialog), not on the stack
              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #9

              @JKSH said in A bug was found when calling qApp->quit():

              Because your code will try to delete the dialog twice. See https://doc.qt.io/qt-5/objecttrees.html#construction-destruction-order-of-qobjects

              Pardon? Why, where?

              You must do one of the following:

              • Do not set a parent for your dialog, OR

              Parents go beyond the simple memory management, they're also responsible for drawing surfaces (a.k.a. window handles/native windows). So now you should explain, why "you must not" ...

              • Allocate the dialog on the heap (by calling new QDialog), not on the stack

              ... nope!

              Read and abide by the Qt Code of Conduct

              JKSHJ 1 Reply Last reply
              0
              • P Pleafles

                @kshegunov Thank you very much, it works normally, but there will be problems with its use:

                ...
                QApplication a(argc, argv);
                QMainWindow window;
                window.show();

                    QDialog dialog(&window);
                    QTimer::singleShot(5000, [] () -> void  {
                        qApp->quit();
                    });
                    dialog.exec();
                
                return a.exec();
                

                It's strange why this doesn't work,and and it will crash...

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #10

                @Pleafles said in A bug was found when calling qApp->quit():

                It's strange why this doesn't work,and and it will crash...

                No offence, but I don't feel like guessing. Please get a stack trace and post it here.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0
                • kshegunovK kshegunov

                  @JKSH said in A bug was found when calling qApp->quit():

                  Because your code will try to delete the dialog twice. See https://doc.qt.io/qt-5/objecttrees.html#construction-destruction-order-of-qobjects

                  Pardon? Why, where?

                  You must do one of the following:

                  • Do not set a parent for your dialog, OR

                  Parents go beyond the simple memory management, they're also responsible for drawing surfaces (a.k.a. window handles/native windows). So now you should explain, why "you must not" ...

                  • Allocate the dialog on the heap (by calling new QDialog), not on the stack

                  ... nope!

                  JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #11

                  @kshegunov said in A bug was found when calling qApp->quit():

                  @JKSH said in A bug was found when calling qApp->quit():

                  Because your code will try to delete the dialog twice. See https://doc.qt.io/qt-5/objecttrees.html#construction-destruction-order-of-qobjects

                  Pardon? Why, where?

                  My bad, my assessment was wrong. Double-deletion won't happen here since the QDialog will be deleted before the QMainWindow.

                  So the problem is somewhere else.

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  kshegunovK 1 Reply Last reply
                  0
                  • JKSHJ JKSH

                    @kshegunov said in A bug was found when calling qApp->quit():

                    @JKSH said in A bug was found when calling qApp->quit():

                    Because your code will try to delete the dialog twice. See https://doc.qt.io/qt-5/objecttrees.html#construction-destruction-order-of-qobjects

                    Pardon? Why, where?

                    My bad, my assessment was wrong. Double-deletion won't happen here since the QDialog will be deleted before the QMainWindow.

                    So the problem is somewhere else.

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #12

                    @JKSH said in A bug was found when calling qApp->quit():

                    So the problem is somewhere else.

                    Indeed. My speculation is that the local event loop is run before the global one, but ... a trace is the way to go, as usual.

                    Read and abide by the Qt Code of Conduct

                    P 1 Reply Last reply
                    0
                    • kshegunovK kshegunov

                      @JKSH said in A bug was found when calling qApp->quit():

                      So the problem is somewhere else.

                      Indeed. My speculation is that the local event loop is run before the global one, but ... a trace is the way to go, as usual.

                      P Offline
                      P Offline
                      Pleafles
                      wrote on last edited by
                      #13

                      @kshegunov I found the problem, as you said, the local event loop is run before the global one(QMessageBox::info() is called in constructor),but why? Where can I explain?

                      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