Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for WebAssembly
  4. How to handle closeEvent with the QDialog .open() instead of .exec()?
Forum Update on Monday, May 27th 2025

How to handle closeEvent with the QDialog .open() instead of .exec()?

Scheduled Pinned Locked Moved Unsolved Qt for WebAssembly
5 Posts 3 Posters 1.4k 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.
  • D Offline
    D Offline
    DZucchetti
    wrote on 26 Dec 2020, 11:37 last edited by DZucchetti
    #1

    Currently within our application, when the window receives a windows closeEvent we display MessageBox, with questions for Save, Discard, Abort so that the user can save the changes.

    In webassembly the widgets .exec() function does not work (see https://bugreports.qt.io/browse/QTBUG-64020).
    With wasm the .exec() function is not available, so it is not possible to block the event queue.
    We need to use the .open() function, so that the closeEvent return before the user has answered the question.
    So we have tried the following:
    • Display a messageBox connected to a lambda function.
    • Call the msg.open()
    • Ignore the event so that the windows does not close
    • In the lambda function that respond to the event QMessageBox::Save
    save the data and close the windows.
    But we have a further problem due to the fact that we use and MDI (multiple document interface). We can have different windows open and the user could cancel the saving of just one window.

    The QDialog documentation advice not to use the exec() function
    “Note: Avoid using this function; instead, use open(). Unlike exec(), open() is asynchronous, and does not spin an additional event loop. This prevents a series of dangerous bugs from happening (e.g. deleting the dialog's parent while the dialog is open via exec()). When using open() you can connect to the finished() signal of QDialog to be notified when the dialog is closed.”

    We would like to know what approach do you suggest to follow in order to use asynch programming within the closeEvent and get rid of the dialog exec() function?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 26 Dec 2020, 12:01 last edited by
      #2

      Hi and welcome to devnet,

      Connect the accepted and rejected signals in order to act properly in each cases.

      You have these information in the exec method documentation.

      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
      • D Offline
        D Offline
        DZucchetti
        wrote on 28 Dec 2020, 11:01 last edited by
        #3

        We have already used the accepted and rejected signals, but the problem is that at this time the closeEvent message queue is already terminated.

        The issue is quite complex and you find an extensive information (but not related to QT) on:
        https://stackoverflow.com/questions/16656523/awaiting-asynchronous-function-inside-formclosing-event

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jeremy_k
          wrote on 30 Dec 2020, 07:21 last edited by
          #4

          Some of the details of the description are confusing to me, but the general problem does not sound difficult. The desired outcome might be accomplished with:

          class Widget : public QWidget {
              bool delayClose = true;
              QDialog *closeDialog;
          
              Widget(QWidget *parent = nullptr) : QWidget(parent) {
                  connect(closeDialog, QDialog::accepted,
                          [&this]() {
                              this->delayClose = false; // closing is now possible
                              this->close();            // close
                          }
                  );
              }
          
              void closeEvent(QCloseEvent *event) {
                  if (delayClose) {
                      event->ignore();     // Don't close now
                      closeDialog->show(); // Ask the user to make closing possible 
                  }
                  else
                      QWidget::closeEvent(event);
          }
          

          Replace the setting of delayClose to false with an appropriate heuristic, possibly calculated in the event handler.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 30 Dec 2020, 07:37 last edited by
            #5

            Just to be sure I understand the issue: you may have multiple documents to save and each asks to save separately ?

            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

            1/5

            26 Dec 2020, 11:37

            • Login

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