我有一个 Button 点击后连接到 myfunction,见下文。
我正在使用 QEventLoop,因为我正在等待一个事件,它会调用 _myEventLoop.exit(0);
当我连续两次单击按钮而不等待事件发生时,我收到错误消息:
QEventLoop::exec: instance 0x22f47378 has already called exec()
我在调用 exec() 之前调用了 exit(1) 但似乎这个 exit 立即返回,然后函数 passe 通过 exec(),因为它仍在运行 return 1,myfunction 返回,然后才返回第一个exec() 返回 1。
在继续之前,我如何确定之前对 exec() 的调用已经返回?
void myfuntion()
{
_myEventLoop.exit(1);
if(_myEventLoop.exec() == 0)
{
// DoSomething
}
}
最佳答案
您可以调用QEventLoop::processEvents
来处理任何未决事件。
你的代码将是这样的:
void myfuntion()
{
_myEventLoop.exit(1);
_myEventLoop.processEvents();
if(_myEventLoop.exec() == 0)
{
// DoSomething
}
}
关于c++ - QEventLoop 之前已经调用了 exec() 甚至 exit(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32828106/