-1

I would like to call a function after x seconds and another after y seconds etc.. The number of seconds to wait before calling the function is stored in a QTime

I thought launching a timer and when the value of this QTimer has the value of one of QTime, call the function, but I dont' know at all how to do ..

Here is my idea in "pseudo-code" :

QTime time1(0, 0, 10); // 00:00:10
QTime time2(0, 0, 15); // 00:00:15

// Init a QTimer

if (QTimer.value == time1.value)
    function1();
if (QTimer.value == time2.value)
    function2();

How I can do that ?

Thanks for your help

R3J3CT3D
  • 226
  • 2
  • 10
  • QTimer has [isActive](http://doc.qt.io/qt-5/qtimer.html#isActive) and [setInterval](http://doc.qt.io/qt-5/qtimer.html#interval-prop). You can use those functions to do what you need with `ms` granularity. – LPs Oct 26 '16 at 09:05
  • 2
    Possible duplicate of [How to use QTimer](http://stackoverflow.com/questions/11651852/how-to-use-qtimer) – LPs Oct 26 '16 at 09:06
  • Timers are not meant to be used that way. Just start them and connect your function to the timeout signal. – Hayt Oct 26 '16 at 09:10

1 Answers1

0

The easiest way is to simply use two QTimer objects, each with the interval for the respective function.

Another option is a single timer that fires at the rate of the lowest common denominator, in your case 5 seconds, and call each function at the appropriate count. I.e. function1 every second timeout, function2 every third timeout.

Another option is to calculate the time until the next event and the start the timer with that interval.

Kevin Krammer
  • 5,159
  • 2
  • 9
  • 22