[PyQt] QThread::run does not worl as expected

David Mugnai dvd at develer.com
Thu Jun 17 13:42:46 BST 2010


Hi phil,

according to the docs, the QThread::run method should call exec (and
thus start a new event loop), but this code doesn't work (it never ends)


> #!/usr/bin/env python
> #-*- coding: utf-8 -*-
> 
> from PyQt4.Qt import *
> 
> app = QApplication([])
> 
> class Start(QEvent):
>     def __init__(self):
>         QEvent.__init__(self, QEvent.User)
>     
> class Receiver(QObject):
>     def doSomething(self):
>         print 'doSomething'
>         qApp.quit()
> 
> class Main(QObject):
>     def test(self):
>         r = self._r = Receiver()
>         t = self._t = QThread()
>         r.moveToThread(t)
>         QObject.connect(self, SIGNAL('test'), r.doSomething)
>         t.start()
>         self.emit(SIGNAL('test'))
> 
> main = Main()
> main.test()
> app.exec_()

If, intead of a QThread, I use a class like this:

> class MyThread(QThread):
>     def run(self):
>         return self.exec_()


all works as expected.


best regards
david
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100617/f67c809b/attachment.pgp>


More information about the PyQt mailing list