我知道,那里有一些类似的问题,但我找不到能帮助我的具体答案.所以这是我的问题:
我在一个应用程序上工作,在启动时执行一些gui-initialisations.我要做的一件事就是打电话
NetworkConfigurationManager::updateConfigurations ()
Run Code Online (Sandbox Code Playgroud)
这是一个异步调用updateCompleted(),在完成后发出信号.问题是,我所有其他的gui-initialisations必须等到updateConfigurations()完成.
所以我能做的就是这样:
MyApp::MyApp(QWidget *parent) : ....
{
doSomeInits();
//Now connect the signal we have to wait for
connect(configManager, SIGNAL(updateCompleted()), this, SLOT(networkConfigurationUpdated()));
configManager->updateConfigurations(); //call the async function
}
void MyApp::networkConfigurationUpdated()
{
doSomething();
doRemainingInitsThatHadToWaitForConfigMgr();
}
Run Code Online (Sandbox Code Playgroud)
拆分初始化对我来说似乎不是一个好方法.我认为它使代码更难阅读 - 内容应该保持在一起.另一件事是:因为updateConfiguration()是异步的,用户将能够使用GUI,它还没有给他任何信息,因为我们正在等待updateCompleted().
那么有一种方法可以updateCompleted()在应用程序继续之前等待信号吗?
喜欢:
MyApp::MyApp(QWidget *parent) : ....
{
doSomeInits();
//Now connect the signal we have to wait for
connect(configManager, SIGNAL(updateCompleted()), this, SLOT(doSomething()));
???? //wait until doSomething() is done.
doRemainingInitsThatHadToWaitForConfigMgr(); …Run Code Online (Sandbox Code Playgroud) 在他的Qt事件循环,网络和I/O API谈话中,Thiago Macieira提到QEventLoop应该避免嵌套:
QEventLoop用于嵌套事件循环...如果可以的话,请避免使用它,因为它会产生许多问题:事情可能会重新进入,新的套接字或定时器激活是你没想到的.
任何人都可以扩展他指的是什么吗?我维护了许多使用模态对话框的代码,这些代码在exec()调用时在内部嵌套一个新的事件循环,因此我非常有兴趣知道这可能导致什么样的问题.