1

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

1 回答 1

1

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

如果你想sender()在你的类中使用,只需让它继承自QObject.

于 2013-05-05T20:56:02.313 回答