[PyQt] Question about QTimer and QThreads

Ingmar Steen iksteen at gmail.com
Sun Oct 28 22:39:27 GMT 2007


On 10/28/07, Darren Dale <darren.dale at cornell.edu> wrote:
> 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_()
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>


This question has been answer really recently on this list. Instead of
calling the thread object's exec_() function, call the thread object's
start() function: a.start() instead of a.exec_()

Ingmar


More information about the PyQt mailing list