QTimer启动功能究竟发生了什么
What exactly happens with QTimer start function?
我有以下代码:
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()
以实现可移植性,即使在某些平台上没有必要。
相关文章:
- 此测试()中发生了什么意外过程?为什么总是覆盖 ch[0 1 2..]?
- 这C++代码中发生了什么C++(指数函数)
- 哪种方式更快?究竟发生了什么,我们没有看到什么?
- 从"LLONG_MAX 秒"构造 std::chrono::毫秒变量时发生了什么?
- 这个片段中关于 n 在 pc[i] 中的表示发生了什么
- istringstream,num1 和 num2 在这里发生了什么?
- 究竟发生了什么,我们需要在 c++ 中双重调度/访客
- 我是否能够确定在部署一个程序后发生了什么,我在数组末尾写入?
- 这个阶乘程序内部发生了什么?
- C++ - *(int**) 地址?这里发生了什么?
- C++用用户输入在循环中填充 char 数组:输入整个字符串时到底发生了什么?
- 让对象知道它创建的 show 函数中发生了什么
- 这里的矢量数组发生了什么?
- 增加顶级常量指针时发生了什么
- 当另一端将其关闭时,插座发生了什么
- C :我堆栈顶部发生了什么
- 此递归中发生了什么?我需要仅使用递归/无循环来计算和备份
- 这个 std::vector 构造函数中发生了什么
- 编译器认为 int 是一个字符串.发生了什么事情
- 无法运行简单的 std::async 和 std::future 测试程序。错误:"has initializer but incomplete type."发生了什么?