是否可以使用新样式pyqtSignal发现信号的发送者?

时间:2013-05-05 20:15:07

标签: python multithreading qt pyqt

以下面的代码为例,我想知道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()

1 个答案:

答案 0 :(得分:1)

sender()方法适用于从QObject派生的所有类,但您的Manager类不是QObject的子类。

如果您想在课堂上使用sender(),请将其从QObject继承。