I know, there are some similar questions to the following out there, but I couldn\'t find a concrete answer that helps me. So here\'s my problem:
I work on an applic
The way to do this is to use nested event loops. You simply create your own QEventLoop, connect whatever signal you want to wait for to the loop's quit()
slot, then exec()
the loop. This way, once the signal is called, it will trigger the QEventLoop's quit()
slot, therefore exiting the loop's exec()
.
MyApp::MyApp(QWidget *parent) : ....
{
doSomeInits();
{
QEventLoop loop;
loop.connect(configManager, SIGNAL(updateCompleted()), SLOT(quit()));
configManager->updateConfigurations();
loop.exec();
}
doReaminingInitsThatHadToWaitForConfigMgr();
}
Working from chalup's answer, if you are going to be waiting for a user-noticeable time, you might want to show a progress bar instead (or a splash screen, perhaps).
MyApp::MyApp(QWidget *parent) : ....
{
doSomeInits();
{
QSpashScreen splash;
splash.connect(configManager, SIGNAL(updateCompleted()), SLOT(close()));
configManager->updateConfigurations();
splash.exec();
}
doReaminingInitsThatHadToWaitForConfigMgr();
}
Another solution could be to use QSignalSpy::wait(). This function was introduced in Qt 5 and does exactly what you want.
The only problem I see with that class is that it is provided in the QtTest module. In my case I find it very useful when testing code, but it might be not the best solution for production code.