python - pyqt timer does not return a value or an error -
i trying use timer pyqt. code below, not print , not error. know wrong?
thanks
import functools pyqt4.qtcore import qtimer def ontimer(initparams): print initparams print "here" # code here... def update(): print "upd" myinitparams = "init!" timercallback = functools.partial(ontimer, myinitparams) mytimer = qtimer() mytimer.timeout.connect(timercallback) mytimer.start(1000) #once sec t = qtimer() t.start(500) t.timeout.connect(update) edit update:
so snip of full code im using, bit more complicated , maybe different problem, result same.
the structure this:
from pyqt4 import qtcore, qtgui # import ui classes ui directory in package ui.moangui import ui_moanwindow class moanwin(qtgui.qdialog, ui_moanwindow): def __init__(self, parent=none): super(moanwin, self).__init__(parent=parent) self.setupui(self) self.playmarkers.clicked.connect(self._playmarkers) self.connect(self,qtcore.signal("playmarker"),playmarkercall) #marker stop self.stopmarkers.clicked.connect(self._stopmarkers) self.connect(self,qtcore.signal("stopmarker"),stopmarkercall) def _stopmarkers(self): arg = "stop" self.emit(qtcore.signal("stopmarker"),arg) def _playmarkers(self): fps = self.fpsbox.value() starttime = self.fromtime.value() endtime = self.totime.value() arg = [fps,starttime,endtime] self.emit(qtcore.signal("playmarker"),arg) def stopmarkercall(arg): #i want stop timer here def playmarkercall(arg): #i tried this, again, nothing happens, no error myinitparams = "init!" timercallback = functools.partial(timercall, myinitparams) mytimer = qtcore.qtimer() mytimer.timeout.connect(timercallback) mytimer.start(1000) #once sec #i imagine this: mytimer = qtcore.qtimer() in range(start,end): # want connect view marker function need pass variable mytimer.timeout.connect(viewmarkercall(i)) mytimer.start(1000) def timercall(args): print "here", args def show(): global globalmoanwindow app = qtgui.qapplication(sys.argv) globalmoanwindow = moanwin() globalmoanwindow.show() sys.exit(app.exec_()) return globalmoanwindow show() my goal have button click play, , stuff happens in qtgraphic widget @ time interval, stop button stops playing. found functools question on here, im not sure if correct thing do.
to correctly use qt, need set qtgui.qapplication or qtcore.qcoreapplication , use it's event loop process events.
this should work:
#from utils import sigint import functools pyqt4.qtcore import qtimer, qcoreapplication app = qcoreapplication([]) def ontimer(initparams): print initparams print "here" # code here... def update(): print "upd" myinitparams = "init!" timercallback = functools.partial(ontimer, myinitparams) mytimer = qtimer() mytimer.timeout.connect(timercallback) mytimer.start(1000) #once sec t = qtimer() t.start(500) t.timeout.connect(update) # use timer stop event loop after time stoptimer = qtimer(timeout=app.quit, singleshot=true) stoptimer.start(4000) app.exec_() edit:
in updated version don't keep reference timer object create in playmarkercall method. when mytimer goes out of scope, destroyed (together underlying c++ qt object), that's why timer never fires.
, can't pass parameters when connect signal slots, @ time set connection. parameters can passed when signal emitted, create class derived qtimer , override it's timerevent method emit signal arguments specified when instantiating timer.
Comments
Post a Comment