What happens if I have multiple duplicate QTimer

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

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

I want to know after 20 milliseconds which one of slot1 and slot2 first executes? and how can I get the loop cycle to start slot2 and then slot1 when they overlap. ( slot2 more important for me to work on time)

+6
source share
1 answer

There is no guarantee that slots in two timers will be called up with specific orders. This is due to the fact that you run them at different times, and QTimer also has millisecond accuracy at best by setting this:

 timer.setTimerType(Qt::PreciseTimer); 

The default value is Qt::CoarseTimer , which causes accuracy within 5% of the required interval.

About your case, if you want to call slot2 and slot1 so that you can call them in the slot connected to the timer with an interval of 10:

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

Source: https://habr.com/ru/post/971030/


All Articles