我想知道QTimer一旦超时就会继续计算。
例如,假设我有一个每500ms超时的QTimer。 让我们说当这个特定的QTimer超时时,我的程序恰好在我所做的某些功能中(不在事件循环中),程序从我的函数到事件循环需要10ms。 这是否意味着下次QTimer超时时间为1010ms?
如果是这种情况,有没有办法解决它?
谢谢。
编辑:
I would like to know if a QTimer will keep on counting once it has timed out.
For example, let's say I have a QTimer that times out every 500ms. Let's say when this particular QTimer times out, my program happens to be in some function that I have made (not in the event loop) and it takes 10ms for the program to get from my function, to the event loop. Does that mean that the next time the QTimer times out will be at 1010ms?
If this is the case, is there a way to get around it?
Thanks.
Edit:
最满意答案
我使用多个计时器但使用timerEvent ( QObject::setTimer() )进行了一些测试。
当然,如果你有多个定时器在某个时刻发生干扰(在同一时间内完全勾选),你的所有doStuffThatTake10ms代码都将“排队”......但是定时器的绝对精度应该随着时间的推移而保持不变。 这里有一些代码可以试试。
#ifndef MULTITIMER_H #define MULTITIMER_H #include <QObject> #include <QTime> class MultiTimer : public QObject { Q_OBJECT public: explicit MultiTimer(QObject *parent = 0); void timerEvent(QTimerEvent *event); private: int timerId[4]; int interval[4]; int count[4]; QTime absoluteTimer; }; #endif // MULTITIMER_H实施.cpp
#include "MultiTimer.h" #include <QTimerEvent> #include <QTime> #include <QDebug> MultiTimer::MultiTimer(QObject *parent) : QObject(parent) { interval[0] = 500; interval[1] = 1000; interval[2] = 1500; interval[3] = 2000; for( int i = 0; i < 4; i++) timerId[i] = startTimer(interval[i]); for( int i = 0; i < 4; i++) count[i] = 0; absoluteTimer.start(); } void MultiTimer::timerEvent(QTimerEvent *event) { int id = -1; for( int i = 0; i < 4; i++){ if( event->timerId() == timerId[i]){ id = i; count[id]++; break; } } if( id != -1) { qDebug() << "timer" << id << "interval" << interval[id] << "count" << count[id] << "total" << count[id]*interval[id] << "reference" << absoluteTimer.elapsed(); usleep(10000); } }要试一试,请创建a = QApllication()和一个MultiTimer对象并触发a.exec() 。
在linux上1分钟后的结果
timer 1 interval 1000 count 59 total 59000 reference 59010 timer 0 interval 500 count 119 total 59500 reference 59500 timer 0 interval 500 count 120 total 60000 reference 60000 timer 1 interval 1000 count 60 total 60000 reference 60010 timer 3 interval 2000 count 30 total 60000 reference 60021 timer 2 interval 1500 count 40 total 60000 reference 60031 timer 0 interval 500 count 121 total 60500 reference 60500正如你所看到的那样, timer 0第121个滴答是正确的,准时为60500ms ...但是在60000ms所有定时器都会发生冲突,造成延迟执行。
I made some test using multiple timers but with timerEvent (QObject::setTimer()).
The thing is, of course if you have multiple timers that interfere at some point (tick exactly in the same time) all your doStuffThatTake10ms code will be 'queued' ... But the absolute precision of timers should remain over time. Here some code to try it out.
#ifndef MULTITIMER_H #define MULTITIMER_H #include <QObject> #include <QTime> class MultiTimer : public QObject { Q_OBJECT public: explicit MultiTimer(QObject *parent = 0); void timerEvent(QTimerEvent *event); private: int timerId[4]; int interval[4]; int count[4]; QTime absoluteTimer; }; #endif // MULTITIMER_HImplementation .cpp
#include "MultiTimer.h" #include <QTimerEvent> #include <QTime> #include <QDebug> MultiTimer::MultiTimer(QObject *parent) : QObject(parent) { interval[0] = 500; interval[1] = 1000; interval[2] = 1500; interval[3] = 2000; for( int i = 0; i < 4; i++) timerId[i] = startTimer(interval[i]); for( int i = 0; i < 4; i++) count[i] = 0; absoluteTimer.start(); } void MultiTimer::timerEvent(QTimerEvent *event) { int id = -1; for( int i = 0; i < 4; i++){ if( event->timerId() == timerId[i]){ id = i; count[id]++; break; } } if( id != -1) { qDebug() << "timer" << id << "interval" << interval[id] << "count" << count[id] << "total" << count[id]*interval[id] << "reference" << absoluteTimer.elapsed(); usleep(10000); } }To try it out create a = QApllication() and a MultiTimer object and fire a.exec().
Result after 1 minute on linux
timer 1 interval 1000 count 59 total 59000 reference 59010 timer 0 interval 500 count 119 total 59500 reference 59500 timer 0 interval 500 count 120 total 60000 reference 60000 timer 1 interval 1000 count 60 total 60000 reference 60010 timer 3 interval 2000 count 30 total 60000 reference 60021 timer 2 interval 1500 count 40 total 60000 reference 60031 timer 0 interval 500 count 121 total 60500 reference 60500As you can see the 121th tick of timer 0 is right on time at 60500ms ... but at 60000ms all timers collide creating delayed execution.
更多推荐
发布评论