Under Qt5, how to wait until the main event loop has executed all slots that are connected to a specific widget?
Specifically, in the following example
class MyWidget : public QWidget {
~MyWidget() {
action_A();
}
void myclose() {
...
close();
}
};
...
auto* w = new MyWidget;
...
w->close();
action_B();
...
I want the two functions
action_A();
action_B();
to be executed in the above order. However, without further precaution, they are executed in the reverse order.
QWidget::close()
emits a signal which ultimately triggers deletion of the MyWidget
instance. However, to the best of my understanding, this will only happen after control has returned to the main event loop. In any case, it will not happen before we call action_B()
.
Related discussions that don't help:
- Wait until QWidget closes, specific for a
QDialog
, accepted answer advises to useQDialog::exec()
. However, the Qt5 docs recommend to avoidQDialog::exec()
in favor ofQDialog::open()
. For this and for other reasons I cannot avoid the call toQDialog::close()
. - PyQt: Wait until widget closes, also depends on
QDialog::exec()
. - Does QThread::quit() discard all events in EventQueue?, promoted by @Marek R, involves QThread multithreading, and is too complicated to be of help here