以下面的代码为例,我想知道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()
答案 0 :(得分:1)
sender()
方法适用于从QObject
派生的所有类,但您的Manager
类不是QObject
的子类。
如果您想在课堂上使用sender()
,请将其从QObject
继承。