[review] [PyKDE] QTimer: Pause and resume?

Jim Bublitz jbublitz at nwinternet.com
Fri Jul 30 20:59:01 BST 2004


On Friday 30 July 2004 08:59, Karl wrote:

> I am currently using quite a lot of timers to manage the behaviour of
> several nodes on a canvas.

> My situation is, that the user can click "Pause", currently I stop all
> timers. I would like to restart them at the point they were, when paused,
> therefore I would need the remaining time for the timer to shoot the
> signal.

> I could not find out using QTimer directly, any ideas????

from qt import QTimer
import time

class QTimerWithPause (QTimer):
  def __init__ (self, parent = None, name = ""):
      QTimer.__init__ (self, parent, name)

      self.startTime = 0
      self.interval    = 0

  def startTimer (self, interval):
      self.interval    = interval
      self.startTime = time.time ()

      QTimer.start (self, interval, True) # one-shot

  def pause (self):
      if self.isActive ():
          self.stop ()

          elapsedTime    = self.startTime - time.time ()
	  self.startTime -= elapsedTime

          # time() returns float secs, interval is int msec
          self.interval -= int (elapsedTime*1000))

  def resume (self):
      if not self.isActive ():
          self.start (self.interval)

I think you can reload/restart single-shot timers. If not, you'll need to 
change this a little, but the principle should still work. I haven't tried it 
though. Should be accurate to a few ms (round-off and latency errors).
It also doesn't save the original startTime and interval values.

Jim




More information about the PyQt mailing list