[PyQt] Question about QTimer and QThreads

Darren Dale darren.dale at cornell.edu
Sun Oct 28 22:31:25 GMT 2007


I am trying to understand how to run a second event loop using a qthread. The 
Qt docs indicate this is possible, but I haven't found any examples. I have a 
simple example that I think should work, but doesn't. When I run my thread's 
exec_(), it blocks. The actual application I am writing calls the qApp's 
exec_ before the thread's exec_, but even then the thread's exec_ blocks 
further execution. Does anyone know how to do this?

Thanks,
Darren



from PyQt4 import QtGui, QtCore
import time

class Dispatcher(QtCore.QThread):
    
    def __init__(self, parent=None):
        QtCore.QThread.__init__(self, parent)
        
        self.timer = QtCore.QTimer(self)
        self.connect(self.timer,
                     QtCore.SIGNAL("timeout()"),
                     self.update)
        self.timer.start(20)

    def update(self):
        self.emit(QtCore.SIGNAL('update(string)'), str(time.time()))

app = QtGui.QApplication(['test'])
text = QtGui.QLabel('Hi')
a = Dispatcher()
text.connect(a,
               QtCore.SIGNAL('update(str)'),
               text,
               QtCore.SLOT('setText(string)'))

a.exec_()

text.show()
app.exec_()


More information about the PyQt mailing list