[PyQt] timers from mainwindow

Sebastian Elsner sebastianelsner at freenet.de
Tue Sep 14 18:28:37 BST 2010


Hello,

I would like to start a QTimer from a mainwindow's menu, creating a new 
instance of a custom class. See the example below. All works except the 
timer's callback is never called. Asking if the timer is active returns 
True. I ran out of ideas. Maybe I misunderstand how timers work?! Any 
help appreciated.

from PyQt4 import QtCore, QtGui
from PyQt4 import uic
import sys


class Timers(QtCore.QObject):
	def __init__(self, parent=None):
		QtCore.QObject.__init__(self)
		print "Timers instance created"
		self.timer = QtCore.QTimer()
		self.connect(self.timer, QtCore.SIGNAL("timeout()"), self.callback)
		self.timer.start(1000)
		print self.timer.isActive()
		
	def callback(self):
		print "timer called"

class MainWindow(QtGui.QMainWindow):

	def __init__(self):
		QtGui.QMainWindow.__init__(self)
		self.menubar = QtGui.QMenuBar(self)
		self.menuMenu = QtGui.QMenu(self.menubar)
		self.setMenuBar(self.menubar)
		self.actionDOIT = QtGui.QAction(self)
		self.menuMenu.addAction(self.actionDOIT)
		self.menubar.addAction(self.menuMenu.menuAction())
		self.menuMenu.setTitle("menu")
		self.actionDOIT.setText("DO IT")
		self.connect(self.actionDOIT, QtCore.SIGNAL("triggered()"), self.doit)
		
	def doit(self):
		Timers()
		
if __name__ == "__main__":
	app = QtGui.QApplication(sys.argv)
	mainWindow = MainWindow()
	mainWindow.show()
	sys.exit(app.exec_())

Thank you

Sebastian

		


More information about the PyQt mailing list