Qt4:未解决的计时器事件 [英] Qt4: Unresolved timer events
问题描述
迷你样本文件main.cpp
:
#include <iostream>
#include <QCoreApplication>
class MyApp : public QCoreApplication
{
private:
int m_idtimer;
public:
MyApp(int nargs, char* argc[]) : QCoreApplication(nargs, argc)
{
m_idtimer = startTimer(3000); // 3 segs.
}
protected:
void timerEvent(QTimerEvent* e)
{
char c = '\0';
std::cout << "timerEvent" << std::endl;
std::cin >> c;
if (c == 'q') {
killTimer(m_idtimer);
quit();
}
}
};
int main(int nargs, char* argc[])
{
MyApp app(nargs, argc);
return app.exec();
}
超迷你Makefile
:
LDFLAGS = -I/usr/include/qt4 -I/usr/include/qt4/QtCore
LDLIBS = -lQtCore
编译和执行:
$ make main
g++ -I/usr/include/qt4/QtCore main.cpp -lQtCore -o main
$ ./main
timerEvent
1
timerEvent
2
timerEvent
3
timerEvent
q
$
好的,然后是我的问题.我制作此示例的目的是测试计时器事件是否是累积的.
Ok and then, my question. I’ve made this sample with the purpose of testing if timer events are accumulative.
当我执行main
程序时,下一个发生:
When I executing the main
program, the next occurs:
- 第一个
timerEvent
消息在3秒后显示,并且timerEvent()
等待一个字符. - 我直接按
1
. - 3秒后,第二条
timerEvent
消息出现(如预期). - 我等待几秒钟(15或更长时间),然后按
2
- 立即显示第三条消息(累积了一个计时器事件).
- 我立即按
3
. - 3秒后,出现第四条消息(不再累积计时器事件).
- 我按
q
键,程序结束.
- the first
timerEvent
message is shown after 3 seconds, andtimerEvent()
waits a character. - I press
1
inmediatly. - 3 seconds later, the second
timerEvent
message appear (as expected). - I wait some seconds (15 or more) and I press
2
- The third message is shown immediatly (one timer event accumulated).
- I press
3
immediatly. - And 3 seconds later the fourth message appear (no more timer events accumulated).
- I press
q
and the program ends.
问题:为什么没有更多的计时器事件累积?这种行为取决于平台吗?
Question: Why aren there no more timer events accumulated? Does this behaviour depend on the platform?
PD:我的Qt版本是4.8,我的SO Ubuntu 13.04和我的内核(Linux)是3.8.0-19-generic.运行的图形系统是Gnome 3.
PD: My Qt version is 4.8, my SO Ubuntu 13.04, and my kernel (linux) 3.8.0-19-generic. The running graphic system is Gnome 3.
推荐答案
您的15秒等待将不会累积计时器事件,因为您的timerEvent
代码会阻塞等待输入的时间.在输入之前,Qt无法返回其事件循环.当确实返回事件循环时,它会检查经过的时间,并注意到已超过3秒,因此它触发了计时器事件.十五秒过去的事实是无关紧要的.
Your fifteen second wait will not accumulate timer events because your timerEvent
code blocks waiting for the input. Qt can't get back into its event loop until you enter the input. When it does get back to the event loop it checks the elapsed time and notices that more than 3 seconds have elapsed and so it fires off a timer event. The fact that fifteen seconds has elapsed is irrelevant.
这是预期的行为,不会(不应)与平台有关.
This is the expected behaviour and will not (should not) be platform-dependent.
这篇关于Qt4:未解决的计时器事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!