Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if I have several overlapping QTimer

Suppose I have 2 QTimer objects with 10, 20 as their intervals. And suppose I want to run slot1() with timer 1 timeout signal and slot2 with timer 2. So running order of slot1 and slot2 is something like this :

10ms-----20ms-----------30ms----40ms-----  
(slot1) (slot1, slot2) (slot1) (slot1, slot2)...  

I want to know after 20 milliseconds which one of slot1 & slot2 executes at first? and how can I force the event loop to run slot2 and then slot1 when they have overlap.(slot2 is more important for me to run on time)

like image 300
s4eed Avatar asked Jun 19 '14 05:06

s4eed


Video Answer


1 Answers

There is no guarantee that slots in two timers get called with specific orders. This is because you are starting them in different times and also QTimer at best has millisecond accuracy by setting this :

timer.setTimerType(Qt::PreciseTimer);

The default is Qt::CoarseTimer which causes to have an accuracy within 5% of the desired interval.

About your case, if you want to call slot2 and slot1 in order you can call them in a slot connected to a timer with interval of 10 :

void myClass::onTriggered()
{
    if(count % 2==0)
        slot2();
    slot1();

    count++;
}
like image 137
Nejat Avatar answered Sep 20 '22 00:09

Nejat