QTimer启动功能究竟发生了什么

What exactly happens with QTimer start function?

本文关键字:发生了 什么 究竟 功能 启动 QTimer      更新时间:2023-10-16

我有以下代码:

mytimer.cpp

#include "mytimer.h"
#include <QtCore>
MyTimer::MyTimer()
{
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(mySlot()));
timer->start(1000);    
}
void MyTimer::mySlot()
{
qDebug()<<"timer executed";
}

并且在main.cpp

#include <QCoreApplication>
#include "mytimer.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyTimer mtimer;
qDebug()<<"DONE";
return a.exec();
}

现在输出为:

DONE
timer executed
timer executed
...
...
...
...
infinite sequence

我真的很困惑。我们是如何完成主函数的,而SLOT mySlot()的代码仍在执行?

这其中有哪些重要方面?我需要理解的?

还有当我将mytimer.cppmytimer()修改为:时会发生什么变化

MyTimer::MyTimer()
{
timer = new QTimer(this);
QEventLoop eventloop;
connect(timer,SIGNAL(timeout()),this,SLOT(mySlot()));
connect(timer,SIGNAL(timeout()),&eventloop,SLOT(quit()));
timer->start(1000);
eventloop.exec();
}

打印DONE之前执行了一个计时器。具体来说,输出现在变成:

timer executed
DONE
timer executed
timer executed
...
...
...
...
infinite sequence

是什么原因导致执行的单独计时器出现在DONE之上?

否-您的主要功能尚未完成。它调用了.exec(),它将永远不会在您的应用程序中返回。

a.exec()依次处理一个"消息队列",该队列触发调用mySlot()的所有计时器事件。

a.exec启动事件循环。在QApplication::exit()QApplication::quit()或所有窗口关闭之前,它不会返回值。

是什么导致执行的单独计时器出现在DONE之上?

计时器信号总是从最外层的事件循环中发出,因为这是控制轨迹所在的地方(即线程运行的地方,它在Qt内部)。由于您旋转了一个本地事件循环(eventloop.exec();),所以这就是计时器调用的来源。一旦该事件循环完成并且exec()返回,MyTimer构造函数就退出,DONE被打印出来,剩余的计时器调用从主事件循环中发生。

一般来说,嵌套事件循环的代码是坏的,所以如果您发现自己在调用堆栈上有多个exec(),那么您就错了。有几个值得注意的例外:由于OS X API的缺陷,OS X上的本机对话框需要它们自己的嵌套事件循环,而QDrag需要exec()也很可能是由于平台的缺陷,需要exec()以实现可移植性,即使在某些平台上没有必要。