I write a simple demo to check the single-shot timer accuracy. Here below are my codes:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//two singleshot timers
QTimer::singleShot(1000,Qt::PreciseTimer,this,SLOT(myslot1()));
QTimer::singleShot(10000,Qt::PreciseTimer,this,SLOT(myslot2()));
//metrics of timing accuraccy
t = 0;
timer = new QTimer();
timer->setInterval(100);
timer->setTimerType(Qt::PreciseTimer);
connect(timer,SIGNAL(timeout()),this,SLOT(timerhandler()),Qt::DirectConnection);
timer->start();
}
void MainWindow::myslot1()
{
qDebug()<<"myslot1 called: t="<<100*t;
ui->lcdNumber1->display(1);
}
void MainWindow::myslot2()
{
qDebug()<<"myslot2 called: t="<<100*t;
ui->lcdNumber2->display(2);
}
void MainWindow::timerhandler()
{
t++;
ui->lcdNumber->display(t*100);
}
Run these codes gives:
myslot1 called: t= 900
myslot2 called: t= 9900
It seems to me there is a timing error of about 100 ms, for the precise timer. Are I right?? Is the apparent error due to the time interval I set for timer
? According to some post (QTimer not accurate at all?), QTimer may not be so accurate at all.