本文介绍了阻塞等待异步Qt信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,下面有一些类似的问题,但我找不到一个具体的答案帮助我。所以这里是我的问题:



我工作在一个应用程序,启动时做一些gui-initialisations。我要做的事情之一是调用

  NetworkConfigurationManager :: updateConfigurations()

这是一个异步调用,它发出 updateCompleted()完成。问题是,所有其他gui-initialisations必须等待,直到 updateConfigurations()完成。



所以我可以这样做:

  MyApp :: MyApp(QWidget * parent):.... 
{
doSomeInits();
//现在连接我们等待的信号
connect(configManager,SIGNAL(updateCompleted()),this,SLOT(networkConfigurationUpdated());
configManager-> updateConfigurations(); //调用异步函数
}

void MyApp :: networkConfigurationUpdated()
{
doSomething();
doRemainingInitsThatHadToWaitForConfigMgr();
}

分割初始化似乎不是一个好办法。我认为它使代码更难读 - 应该保持在一起。另一件事是:因为 updateConfiguration()异步,用户将能够使用GUI,这不会给他任何信息但是,因为我们正在等待 updateCompleted()



在应用程序继续之前, updateCompleted()信号?



like:

  MyApp :: MyApp(QWidget * parent):.... 
{
doSomeInits
//现在连接我们等待的信号
connect(configManager,SIGNAL(updateCompleted()),this,SLOT(doSomething()))
???? //等到doSomething()完成。
doRemainingInitsThatHadToWaitForConfigMgr();
}

在某些API中有异步函数的阻塞替代, 。



我很感激任何帮助。感谢!

解决方案
  MyApp :: MyApp(QWidget * parent):.... 
{
doSomeInits();
{
QEventLoop loop;
loop.connect(configManager,SIGNAL(updateCompleted()),SLOT(quit()));
configManager-> updateConfigurations();
loop.exec();
}
doReaminingInitsThatHadToWaitForConfigMgr();注意:我没有事件编译这段代码,但IMHO本地QEventLoop是一种方式。


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 application that does some gui-initialisations on start up. One of the things I have to do, is calling

NetworkConfigurationManager::updateConfigurations ()

This is a asynchronous call, which emits the updateCompleted() signal, when it is finished. The problem is, that all my other gui-initialisations have to wait until the updateConfigurations() is finished.

So what I could do would be something like this:

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();
}

To split up the initialisation doesn't seem a good way to me. I think it makes the code much harder to read - inits should remain together. The other thing is: Because updateConfiguration() is asynchronous, the user will be able to use the GUI, which doesn't give him any information yet, cause we are waiting for updateCompleted().

So is there a way to wait for the updateCompleted() signal, before the application continues?

like:

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();
}

In some APIs there are blocking alternatives to asynchronous functions, but not in this case.

I appreciate any help. Thanks!

解决方案
MyApp::MyApp(QWidget *parent) : ....
{
    doSomeInits();
    {
        QEventLoop loop;
        loop.connect(configManager, SIGNAL(updateCompleted()), SLOT(quit()));
        configManager->updateConfigurations();
        loop.exec();
    }
    doReaminingInitsThatHadToWaitForConfigMgr();
}

Note: I haven't event compiled this snippet, but IMHO local QEventLoop is a way to go.

这篇关于阻塞等待异步Qt信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:46