What happens if I have several overlapping QTimer
Asked Answered
R

1

6

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)

Rochkind answered 19/6, 2014 at 5:43 Comment(0)
R
9

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++;
}
Reposeful answered 19/6, 2014 at 6:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.