私は、複数のタイマーを使用して、しかし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
a = QApllication()
とMultiTimer
対象と火a.exec()
を作成し、それを試して実装.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);
}
}
。
結果あなたはtimer 0
の121thティックが60500ms
時間に右である...しかし60000ms
ですべてのタイマーは遅延実行を作成衝突見ることができるように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 60500
1分後。
テストコードをありがとうございます。私は自分のコンピュータ(Windows 7 x64)でテストを実行して、自分のタイマーの結果が本当にタイマーより悪くなっていることを発見しました。私の編集を見れば、500msを見るだけで、それは後で読むことができます。最後に表示されているところでは、開始から約200msです。私はLinuxがもっと正確にそれに驚くことはありません! S.ウィンドウでそれを実行するには、スリープ(10)にスリープ(10000)を変更する必要がありました。私はこれが何かを混乱させないことを望む。 –