That doesn't work for me. I only receive the MainThread print statement in my terminal.
edit: wysota, here is some modified code and the output. Even after the Object class is moved to the Thread class I still get the main thread's ID ... but for me the signal is not being caught by the Object's reportThread method.
from PyQt4 import QtCore
import sys, time
def __init__(self):
def reportThread(self):
print self,
(QtCore.
QThread.
currentThreadId())
def __init__(self):
def reportThread(self):
print self,
(QtCore.
QThread.
currentThreadId())
thr = Thread()
obj = Object()
obj.moveToThread(thr)
print obj, obj.thread(), obj.thread().currentThreadId()
thr.start()
print
( "MainThread "+str
(QtCore.
QThread.
currentThreadId()) )timer.timeout.connect(obj.reportThread)
timer.timeout.connect(thr.reportThread)
timer.start(2000)
t2.timeout.connect(app.quit)
t2.start(10000)
app.exec_()
from PyQt4 import QtCore
import sys, time
class Object(QtCore.QObject):
def __init__(self):
QtCore.QObject.__init__(self)
def reportThread(self):
print self, (QtCore.QThread.currentThreadId())
class Thread(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
def reportThread(self):
print self, (QtCore.QThread.currentThreadId())
app = QtCore.QCoreApplication(sys.argv)
thr = Thread()
obj = Object()
obj.moveToThread(thr)
print obj, obj.thread(), obj.thread().currentThreadId()
thr.start()
timer = QtCore.QTimer()
print ( "MainThread "+str(QtCore.QThread.currentThreadId()) )
timer.timeout.connect(obj.reportThread)
timer.timeout.connect(thr.reportThread)
timer.start(2000)
t2 = QtCore.QTimer()
t2.timeout.connect(app.quit)
t2.start(10000)
app.exec_()
To copy to clipboard, switch view to plain text mode
output:
<__main__.Object object at 0xabd2f8> <__main__.Thread object at 0xabd270> 140273089054464 MainThread 140273089054464
<__main__.Thread object at 0xabd270> 140273089054464
<__main__.Thread object at 0xabd270> 140273089054464
<__main__.Thread object at 0xabd270> 140273089054464
<__main__.Thread object at 0xabd270> 140273089054464
<__main__.Thread object at 0xabd270> 140273089054464
<__main__.Object object at 0xabd2f8> <__main__.Thread object at 0xabd270> 140273089054464 MainThread 140273089054464
<__main__.Thread object at 0xabd270> 140273089054464
<__main__.Thread object at 0xabd270> 140273089054464
<__main__.Thread object at 0xabd270> 140273089054464
<__main__.Thread object at 0xabd270> 140273089054464
<__main__.Thread object at 0xabd270> 140273089054464
To copy to clipboard, switch view to plain text mode
Bookmarks