解决的问题:
很多情况会出现多线程程序,再进行操作时候,其中一个线程的逻辑执行需要另外一个线程的一个信号,那么异步变同步就变得无比重要
如何实现:
使用:QEventLoop类
The QEventLoop class provides a means of entering and leaving an event loop.
QEventLoop类提供了一种进入和离开事件循环的方法。
At any time, you can create a QEventLoop object and call exec() on it to start a local event loop. From within the event loop, calling exit() will force exec() to return.
在任何时候,您都可以创建一个QEventLoop对象并在其上调用exec()来启动一个本地事件循环。 在事件循环中,调用exit()将强制返回exec()。
代码块解析:
QEventLoop q;
QTimer t;
t.setSingleShot(false);
connect(&t, &QTimer::timeout, this, [=](){
//TODO SOMETHING
});
connect(this, SIGNAL(connectStatusChangedSig()), &q, SLOT(quit())); //异步调用完成退出
t.start(50);
q.exec();