[PyQt] Question about QTimer and QThreads

Darren Dale darren.dale at cornell.edu
Sun Oct 28 23:07:51 GMT 2007


On Sunday 28 October 2007 06:39:27 pm Ingmar Steen wrote:
> 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_()

According to the documentation, a QTimer requires an eventloop, or its timeout 
never gets called. The documentation also indicates that QThread.exec needs 
to be called in order to enter the eventloop. Anyway, changing my example to 
use a.show() doesn't work, the thread's event loop is not started. I don't 
see any recent discussion on the list that seems relevent.

Darren


More information about the PyQt mailing list