[PyQt] question about the QThread event loop

Doh-Hyoung Kim kim.dohhyoung at gmail.com
Fri Feb 12 07:02:37 GMT 2010


Hi, I have a question about the QThread event loop.

In order to test the behavior of QThread event loop, I tested the
following code.
Each of thread A, B, C has its own event loop.
Thread B sends a signal to thread A, and the thread C sends a signal
to the thread B,
after the event loops start.

=====================================================================
import time, sys
from PyQt4 import QtCore
from PyQt4 import QtGui

app = QtGui.QApplication(sys.argv)

class MyThread(QtCore.QThread):

    def __init__(self, name, sec):
        super(MyThread, self).__init__()
        self.setObjectName(name)
        self.sec = sec
        self.dict_handler = {}

    def run(self):
        print "'" + self.objectName() + "' is starting in",
QtCore.QThread.currentThreadId()
        time.sleep(self.sec)
        self.emit(QtCore.SIGNAL("mysignal"))
        self.exec_()

    def myhandler(self):
        print "'" + self.objectName() + "' is handling in",
QtCore.QThread.currentThreadId()

a = MyThread('a', 0)
b = MyThread('b', 2)
c = MyThread('c', 4)

a.connect(b, QtCore.SIGNAL("mysignal"), a.myhandler, QtCore.Qt.QueuedConnection)
b.connect(c, QtCore.SIGNAL("mysignal"), b.myhandler, QtCore.Qt.QueuedConnection)

a.start()
b.start()
c.start()

app.exec_()
=====================================================================


and the result was:
=====================================================================
'a' is starting in <sip.voidptr object at 0x01AB1530>             => line 1
'b' is starting in <sip.voidptr object at 0x01AB1530>             => line 2
'c' is starting in <sip.voidptr object at 0x01AB1530>             => line 3
'a' is handling in <sip.voidptr object at 0x01AB1530>             => line 4
'b' is handling in <sip.voidptr object at 0x01AB1530>             => line 5
=====================================================================

I expected the threads in line 1, 2, 3. the main thread, however, I
thought that
the threads in line 4 and 5 must be different from the main thread and
each other.

'Why all the handlers were executed in the same thread?

I will appreciate if anyone answers this question.
Thanks.


More information about the PyQt mailing list