Reputation: 2163
I am trying to use a timer with pyqt. The code is below, but it does not print anything and I do not get an error. Does anyone know what is wrong?
Thanks
import functools
from PyQt4.QtCore import QTimer
def onTimer(initParams):
print initParams
print "HERE"
# your code here...
def update():
print "Upd"
myInitParams = "Init!"
timerCallback = functools.partial(onTimer, myInitParams)
myTimer = QTimer()
myTimer.timeout.connect(timerCallback)
myTimer.start(1000) #once a sec
t = QTimer()
t.start(500)
t.timeout.connect(update)
EDIT UPDATE:
So this is a snip of the full code im using, which is a bit more complicated and maybe is a different problem, but the result is the same.
The structure is like this:
from PyQt4 import QtCore, QtGui
# Import the UI Classes from the ui directory in this package
from 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 to stop the timer here
def PlayMarkerCall(arg):
#I tried this, but again, nothing happens, no error
myInitParams = "Init!"
timerCallback = functools.partial(TimerCall, myInitParams)
myTimer = QtCore.QTimer()
myTimer.timeout.connect(timerCallback)
myTimer.start(1000) #once a sec
#I Imagine something like this:
myTimer = QtCore.QTimer()
for i in range(start,end):
# I want to connect to my view marker function but i need to pass a 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 is to have a button click play, and stuff happens in a qtgraphic widget at a certain time interval, then the stop button stops the playing. I found the functools from another question on here, but im not sure if its the correct thing to do.
Upvotes: 0
Views: 1239
Reputation: 69012
To correctly use Qt, you need to set up a QtGui.QApplication
or QtCore.QCoreApplication
and use it's event loop to process events.
This should work:
#from utils import sigint
import functools
from PyQt4.QtCore import QTimer, QCoreApplication
app = QCoreApplication([])
def onTimer(initParams):
print initParams
print "HERE"
# your code here...
def update():
print "Upd"
myInitParams = "Init!"
timerCallback = functools.partial(onTimer, myInitParams)
myTimer = QTimer()
myTimer.timeout.connect(timerCallback)
myTimer.start(1000) #once a sec
t = QTimer()
t.start(500)
t.timeout.connect(update)
# use a timer to stop the event loop after some time
stopTimer = QTimer(timeout=app.quit, singleShot=True)
stopTimer.start(4000)
app.exec_()
Edit:
In your updated version you don't keep any reference to your timer object you create in the PlayMarkerCall
method. When myTimer
goes out of scope, it is destroyed (together with the underlying C++ Qt object), that's why your timer never fires.
And you can't pass parameters when you connect a signal to a slots, at that time you only set up the connection. Parameters can only be passed when the signal is emitted, to do that you could create a class derived from QTimer
and override it's timerEvent
method to emit a signal with arguments specified when instantiating the Timer.
Upvotes: 2