Qt:等待有超时管理的信号 [英] Qt: waiting for a signal with timeout management

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

问题描述

我正在寻找一个简单的方法来等待一个对象发出信号,使用Qt的超时管理。



有一个简单的方法来使用Qt ?



这是一个应用程序示例:

  QLowEnergyController控制器(remoteDevice); 
controller.connectToDevice();
//现在等待控制器发出连接()1秒超时


方案

根据这篇文章,这里是一个类(封装@EnOpenUK解决方案),并提出

头文件

 

code> #include< QEventLoop>
class WaitForSignalHelper:public QObject
{
Q_OBJECT
public:
WaitForSignalHelper(QObject& object,const char * signal);

//如果信号等待超时则返回false
bool wait();

public slots:
void timeout(int timeoutMs);

private:
bool m_bTimeout;
QEventLoop m_eventLoop;
};

执行档案

  #include< QTimer> 
WaitForSignalHelper :: WaitForSignalHelper(QObject& object,const char * signal):
m_bTimeout(false)
{
connect(& object,signal,& m_eventLoop,SLOT放弃()));
}

bool WaitForSignalHelper :: wait(int timeoutMs)
{
QTimer timeoutHelper;
if(timeoutMs!= 0)//管理超时
{
timeoutHelper.setInterval(timeoutMs);
timeoutHelper.start();
connect(& timeoutHelper,SIGNAL(timeout()),this,SLOT(timeout()));
}
// else,wait for ever!

m_bTimeout = false;

m_eventLoop.exec();

return!m_bTimeout;
}

void WaitForSignalHelper :: timeout()
{
m_bTimeout = true;
m_eventLoop.quit();
}

示例:

  QLowEnergyController控制器(remoteDevice); 
controller.connectToDevice();
WaitForSignalHelper helper(controller,SIGNAL(connected()));
if(helper.wait(1000))
std :: cout<< 接收到信号< std :: endl;
else
std :: cout<< 1秒后没有接收到信号<< std :: endl;

请注意,将超时参数设置为 0 对象等待...可能是有用的。


I'm looking for a simple way to wait for an object to emit signal with timeout management using Qt.

Is there an easy way to do that using Qt classes?

Here is an example of application:

QLowEnergyController controller(remoteDevice);
controller.connectToDevice();
// now wait for controller to emit connected() with a 1sec timeout

解决方案

Based on this post, here is a class (encapsulating @EnOpenUK solution) and proposing a wait function with timeout management.

Header file:

#include <QEventLoop>
class WaitForSignalHelper : public QObject
{
    Q_OBJECT
public:
    WaitForSignalHelper( QObject& object, const char* signal );

    // return false if signal wait timed-out
    bool wait();

public slots:
    void timeout( int timeoutMs );

private:
    bool m_bTimeout;
    QEventLoop m_eventLoop;
};

Implementation file:

#include <QTimer>
WaitForSignalHelper::WaitForSignalHelper( QObject& object, const char* signal ) : 
    m_bTimeout( false )
{
    connect(&object, signal, &m_eventLoop, SLOT(quit()));
}

bool WaitForSignalHelper::wait( int timeoutMs )
{
    QTimer timeoutHelper;
    if ( timeoutMs != 0 ) // manage timeout
    {
        timeoutHelper.setInterval( timeoutMs );
        timeoutHelper.start();
        connect(&timeoutHelper, SIGNAL(timeout()), this, SLOT(timeout()));
    }
    // else, wait for ever!

    m_bTimeout = false;

    m_eventLoop.exec();

    return !m_bTimeout;
}

void WaitForSignalHelper::timeout()
{
    m_bTimeout = true;
    m_eventLoop.quit();
}

Example:

QLowEnergyController controller(remoteDevice);
controller.connectToDevice();
WaitForSignalHelper helper( controller, SIGNAL(connected()) );
if ( helper.wait( 1000 ) )
    std::cout << "Signal was received" << std::endl; 
else
    std::cout << "Signal was not received after 1sec" << std::endl;

Note that setting timeout parameter to 0 makes the object wait for ever...could be useful.

这篇关于Qt:等待有超时管理的信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆