以以下代码为例,我想知道该receiver()
函数是否能够发现 w1 或 w2 是否向其发送了信号?
显然我知道我可以将此信息打包到信号中发送的消息中,但我想知道它是否可用而不这样做。据我所知,该sender()
功能对接收器不可用。
import sys, time, random
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Worker(QThread):
jobDone = pyqtSignal(str)
def __init__(self):
print 'new Worker'
QThread.__init__(self)
def run(self):
time.sleep(2) # give time for event loop to start
print 'thread working'
time.sleep(random.randint(1,8))
self.jobDone.emit('job done')
class Manager:
def __init__(self):
self.w1 = Worker()
self.w1.jobDone.connect(self.receiver)
self.w2 = Worker()
self.w2.jobDone.connect(self.receiver)
self.w1.start()
self.w2.start()
def receiver(self, msg):
print msg
def main():
app = QApplication(sys.argv)
m = Manager()
print 'before event loop'
sys.exit(app.exec_())
if __name__ == '__main__':
main()