k3it
k3it

Reputation: 2770

Force QTimer() timeout in PySide

I have a pySide application which uses QTimer to refresh its status once a minute. In some cases I need to force an immediate update, and then restart the timer

self.timer = QTimer()
self.timer.timeout.connect(self._update_status)
self.timer.start(60 * 1000)

Is there any way to force the timer to expire and emit the timeout signal?

Upvotes: 2

Views: 1472

Answers (1)

ekhumoro
ekhumoro

Reputation: 120798

The cleanest solution seems to be simply this:

        self.timer.start() # restart the timer
        self.timer.timeout.emit() # force an immediate update

It is also possible to force an immediate update by calling setInterval(1), but this has the disadvantage that you would then need to reset the timer interval again in the slot connected to the signal:

        self.timer.setInterval(1) # force an immediate update

    def _update_status(self):
        ...
        if self.timer.interval() == 1:
            self.timer.setInterval(60 * 1000) # reset the interval

(Note that if an interval of zero is used, Qt will only emit the timeout signal once the event queue has been cleared. So, strictly speaking, setInterval(0) wouldn't necessarily force an immediate update).

Upvotes: 2

Related Questions