Important: Please read the Qt Code of Conduct -

Understanding setInterval(int msec) function

  • Hi, this might seem straightforward but I can't exactly understand from the documentation
    I understand this means that we are setting the timeout interval to be x amount of milliseconds. I don't know what that means in practice. What timeout is it going to look for, in what sense, and what will be the effect of the time out? I've never used Qt before and it's been a slow learning curve getting familiar with all the widgets etc. Thank you

  • the purpose of the timer is to execute some method/function at the time it expires or times out. there are singlshot timers that countdown once, and there are repetitive timers that countdown over and over again each time they expire. the interval is just that, the time interval between timeouts or the timer expiring. The reason you might use an interval of zero is to immediately (almost immediately) execute the timeout function to do some initialization, and you would then set a non-zero interval from within the timeout function.

  • @Kent-Dorfman Ah, I see, I found some more information about implementing repetitive QTimers.
    The code I am trying to understand is mainwindow class. It has a private variable QTimer *timer1 and it is initialized in the constructor of MainWindow with the following lines:

    timer1 = new QTimer(this);					
    	timer1->setTimerType(Qt::PreciseTimer); // [ms] accuracy
    	timer1->setInterval(500); /
    	connect(timer1, SIGNAL(timeout(void)), this, SLOT(1Timer(void)));

    1Timer(void) slot function is defined elsewhere.
    So, the section of code I wrote above is saying that every 500ms, 1Timer(void) will be executed. Since this is a private variable which is initialized inside the constructor of MainWindow and MainWindow object is created only once and called only once from the main, does this mean that as long as the MainWindow gui is running, it will execute 1Timer(void) every half a second? I'd think the SIGNAL(timeout(void)) is the signal created by object timer1 which triggers the SLOT of this object to execute 1Timer(void). It seems like these types of arguments are somewhat standard since I found some documentation on it, but I'd like to understand how this works in more detail if you have a second to explain.

  • Your understanding is correct. However, I believe your slot cannot exist as you name it "1Timer" which would be an invalid function name since it starts with a digit. Otherwise it looks ok.

  • @Kent-Dorfman that was my bad, the actual name in the code is different (I didn't want to use the original since it could potentially disclose information I am not allowed to share) and I wasn't creative enough to think of something so I said timer1 haha. The actual variable is all letters :).

Log in to reply