Will QTimer timeout signal be sent after stop() is called?

1.9k views Asked by At

I am working on Qt 5.5 / 4.8 now. I worry about whether timeout() signal could be (occasionally)sent after the timer is explicitly stopped by calling stop() on that instance?

I hope the timeout() signal will be sent just between start() and stop(), but is that the truth?

My situation is: the start() and stop() are both called in main thread, and the timeout() signal is connect as directly connection. (I know that queued connection cannot guarantee the above statement.)

1

There are 1 answers

0
cbuchart On

Quick answer: no, it won't be called on most of the cases, but there is at least one exception.

A situation when it may be called is if you have used queued connections. Here you may get the associated slots to be called after you've stopped if, for example, the QTimer::timeout signal happens to be emitted before stopping the timer.

In the case above slots are queued (but not necessarily called), so it may happen that you call QTimer::stop before slots are actually invoked.

If you use queued connections you can check for QTimer::isActive() at the beginning of the slot (you'll need some way to access the timer of course, maybe as a member of the class).