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 ask on close implementation
Forum Updated to NodeBB v4.3 + New Features

Proper ask on close implementation

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 2.3k 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.
  • C Offline
    C Offline
    CBPeckles
    wrote on last edited by
    #1

    Hi,

    I am implementing a ask on close feature for my application. Atm this is done in the closeEvent function I have overridden in the mainwindow class. Relevant code is as follows:

    void MainWindow::closeEvent(QCloseEvent* event)
    {
        if (QMessageBox::No == QMessageBox::warning(this, 
                                                    qApp->applicationName(), 
                                                    "Are you sure you want to quit?",
                                                    QMessageBox::Yes, 
                                                    QMessageBox::No)) {
            event->ignore();
        } else {
            event->accept();
        }
    }
    

    My problem occurs during shutdown, most notably on windows platforms. Windows will not shutdown the application presumably because it is waiting on user input to confirm the quit. This is undesirable as I would like the application to properly shutdown if the operating system asks it to. Any advice as to how to solve this problem?

    Thanks in advance

    kshegunovK 1 Reply Last reply
    0
    • C CBPeckles

      Hi,

      I am implementing a ask on close feature for my application. Atm this is done in the closeEvent function I have overridden in the mainwindow class. Relevant code is as follows:

      void MainWindow::closeEvent(QCloseEvent* event)
      {
          if (QMessageBox::No == QMessageBox::warning(this, 
                                                      qApp->applicationName(), 
                                                      "Are you sure you want to quit?",
                                                      QMessageBox::Yes, 
                                                      QMessageBox::No)) {
              event->ignore();
          } else {
              event->accept();
          }
      }
      

      My problem occurs during shutdown, most notably on windows platforms. Windows will not shutdown the application presumably because it is waiting on user input to confirm the quit. This is undesirable as I would like the application to properly shutdown if the operating system asks it to. Any advice as to how to solve this problem?

      Thanks in advance

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

      @CBPeckles said in Proper ask on close implementation:

      Windows will not shutdown the application presumably because it is waiting on user input to confirm the quit. This is undesirable as I would like the application to properly shutdown if the operating system asks it to. Any advice as to how to solve this problem?

      Do you mean in the case the OS sends SIGTERM to your application?

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        CBPeckles
        wrote on last edited by
        #3

        Yes, I believe in windows this signal would be WM_QueryEndSession.

        Just to clarify, the intended operation of the program would be

        • User clicks "X" in application bar -> confirm close
        • User clicks a button in the application connected to this->close -> confirm close
        • OS asks program to close -> do not confirm on close and exit program
        kshegunovK 1 Reply Last reply
        0
        • C CBPeckles

          Yes, I believe in windows this signal would be WM_QueryEndSession.

          Just to clarify, the intended operation of the program would be

          • User clicks "X" in application bar -> confirm close
          • User clicks a button in the application connected to this->close -> confirm close
          • OS asks program to close -> do not confirm on close and exit program
          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          Then I suggest not running a local event loop. You'd do something like this:

          event->ignore();
          
          QCoreApplication * app = QCoreApplication::instance();
          
          QMessageBox * box = new QMessageBox(QMessageBox::Question, qApp->applicationName(), "Are you sure you want to quit?", QMessageBox::Yes | QMessageBox::No, this);
          
          QObject::connect(box->button(QMessageBox::Yes), &QAbstractButton::clicked, app, &QCoreApplication::quit);
          QObject::connect(box->button(QMessageBox::No), &QAbstractButton::clicked, box, &QObject::deleteLater);
          QObject::connect(app, &QCoreApplication::aboutToQuit, box, &QObject::deleteLater);
          

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          1
          • C Offline
            C Offline
            CBPeckles
            wrote on last edited by
            #5

            OK I think I understand.

            My problem was that the application was stuck in the QMessageBox's event loop and could not be shutdown, correct?

            Thank you for you help on this. It was much appreciated.

            kshegunovK 1 Reply Last reply
            0
            • C CBPeckles

              OK I think I understand.

              My problem was that the application was stuck in the QMessageBox's event loop and could not be shutdown, correct?

              Thank you for you help on this. It was much appreciated.

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

              @CBPeckles said in Proper ask on close implementation:

              My problem was that the application was stuck in the QMessageBox's event loop and could not be shutdown, correct?

              Yes, this is my theory. I might be wrong, but try the example snippet and if it works, I am right, if not post back here so we can start thinking of something else.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              1
              • C Offline
                C Offline
                CBPeckles
                wrote on last edited by
                #7

                This appears to work as intended now. I believe you forgot a box.show() at the end of the code otherwise nothing happens. In case anyone sees this in the future, revised code, I believe, is as follows:

                void MainWindow::closeEvent(QCloseEvent* event)
                {
                    event->ignore();
                
                    QMessageBox* box = new QMessageBox(QMessageBox::Question, 
                                                       qApp->applicationName(), 
                                                       "Are you sure you want to quit?",
                                                       QMessageBox::Yes | QMessageBox::No, 
                                                       this);
                
                    QObject::connect(box->button(QMessageBox::Yes), &QAbstractButton::clicked, qApp, &QApplication::quit);
                    QObject::connect(box->button(QMessageBox::No), &QAbstractButton::clicked, box, &QObject::deleteLater);
                    QObject::connect(qApp, &QApplication::aboutToQuit, box, &QObject::deleteLater);
                    box->show();
                }
                

                Thanks again!

                kshegunovK 1 Reply Last reply
                3
                • C CBPeckles

                  This appears to work as intended now. I believe you forgot a box.show() at the end of the code otherwise nothing happens. In case anyone sees this in the future, revised code, I believe, is as follows:

                  void MainWindow::closeEvent(QCloseEvent* event)
                  {
                      event->ignore();
                  
                      QMessageBox* box = new QMessageBox(QMessageBox::Question, 
                                                         qApp->applicationName(), 
                                                         "Are you sure you want to quit?",
                                                         QMessageBox::Yes | QMessageBox::No, 
                                                         this);
                  
                      QObject::connect(box->button(QMessageBox::Yes), &QAbstractButton::clicked, qApp, &QApplication::quit);
                      QObject::connect(box->button(QMessageBox::No), &QAbstractButton::clicked, box, &QObject::deleteLater);
                      QObject::connect(qApp, &QApplication::aboutToQuit, box, &QObject::deleteLater);
                      box->show();
                  }
                  

                  Thanks again!

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

                  @CBPeckles said in Proper ask on close implementation:

                  I believe you forgot a box.show() at the end of the code

                  Your belief is well founded, I did forgot it indeed.

                  Read and abide by the Qt Code of Conduct

                  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