Search code examples
c++qtqtimer

Can I use singleShot(true) to make a running QTimer to stop after next shot?


I have a QTimer triggering periodical events.

I don't want to stop the timer directly because I want to sync some special signals to the last event.

Can I achieve this by setting setSingleShot(true) on this QTimer so that its next shot will be a single shot, and the timer stops after the next shot?

Edit: It seems to work. but setSingleShot(true) seems not atomic: I sometimes observe 2 shots before the timmer stops.


Solution

  • Yes. Yes you can. In fact, there is no difference between "regular" and single-shot timers - other than this flag. Each time the timer times out, it check the single-shot flag. If it is set, the timer unregisters itself and thus doesn't fire again until you restart it.