Qt5: How to wait for a signal in a thread?

后端 未结 4 1941
一个人的身影
一个人的身影 2021-01-31 04:46

Probably the title question is not very explicit. I am using Qt5 on Windows7.

In a thread (QThread) at some point, in the \"process()\" function/method, I m

4条回答
  •  感动是毒
    2021-01-31 05:24

    You can use a local event loop to wait for the signal to be emitted :

    QTimer timer;
    timer.setSingleShot(true);
    QEventLoop loop;
    connect( sslSocket, &QSslSocket::encrypted, &loop, &QEventLoop::quit );
    connect( &timer, &QTimer::timeout, &loop, &QEventLoop::quit );
    timer.start(msTimeout);
    loop.exec();
    
    if(timer.isActive())
        qDebug("encrypted");
    else
        qDebug("timeout");
    

    Here it waits until encrypted is emitted or the the timeout reaches.

提交回复
热议问题