What happens with QDialogs when a Qt application's quit() method is called?

833 views Asked by At

I'm looking for a documentation about the things that happen after/while calling the quit() of a Qt application. The question stems from a problem that I had with handling return values of open QDialogs on quit(). I would like to clarify the following sequence:

  1. QCoreApplication::quit() or QApplication::quit() is called
  2. QWidget::closeEvent() is not called for QDialogs. It seems that all open dialogs are automatically closed by calling their reject() method. This is the most important part, is this behavior guaranteed?
  3. The "event-loop-blocking" QDialog::exec() methods return which have to be carefully handled by the caller (access of members of already deleted objects,...).
  4. The aboutToQuit signal is emitted
  5. The destructor of the application is called

So the program flow is: As long as a modal dialog is open the event loop of this dialog is running. When quit() is called QDialog::exec() (the event loop of the modal dialog) is returning which could mean that a lot of additional code is run and even signal/slots could be executed when they are in the same thread. Then the normal event loop is not processed anymore, just aboutToQuit() and the destructors are called.

Is this description correct? Can someone point me to a Qt documentation that explains the interaction of quit() and QDialog? And what happens when I call exec() of a QDialog after a exec() of QDialog returned due to the quit() call? Who is then closing this QDialog?

Thanks, I'm a bit confused about all those interactions.

Edit: It appears that calls to exec() of a QDialog are rejected if the quit() method was called before. So I guess that Qt internally knows that the application is about to quit so all further QDialogs return "rejected" immediately.

2

There are 2 answers

1
Kevin Krammer On BEST ANSWER

I am not sure what you mean with "closeEvent is not called for QDialogs" because that is where it calls reject(): QDialog::closeEvent() code

As for the interaction between various exec() and quit():

  1. QDialog::exec() uses a nested QEventLoop: QDialog::exec() code
  2. QCoreApplication::quit() loops through all nested event loops on tells them to exit: QCoreApplication::exec() code
  3. If a nested event loop is started after the main event loop of the same thread, in the case of a dialog that would be the application's main thread, it will not try "exec" but return immediately. See the first return here: QEventLoop::exec() code
0
Sphics On

It seems logical to me that all open dialogs would have to be "rejected" before the main program can close. (This should be a comment but I don't have enough rep yet >.< ).