[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: [review] [PyKDE] QTimer: Pause and resume?
From:       Jim Bublitz <jbublitz () nwinternet ! com>
Date:       2004-07-30 18:59:32
Message-ID: 200407301200.01891.jbublitz () nwinternet ! com
[Download RAW message or body]

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

_______________________________________________
PyKDE mailing list    PyKDE@mats.imk.fraunhofer.de
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic