'PySide2.QtCore.Signal' 对象没有属性 'connect'

'PySide2.QtCore.Signal' object has no attribute 'connect'

我正在尝试为我的 PySide2 应用程序的 QRunnable 对象创建自定义信号。所有示例都让我通过以下方式创建信号:

class Foo1(QtCore.QObject):

    def __init__():
        super().__init__()
        self.thread = Foo2()
        self.thread.signal.connect(foo)

    def foo():
        # do something


class Foo2(QtCore.QRunnable):

    signal = QtCore.Signal()

但是,我在 self.thread.signal.connect(foo) 上收到以下错误:

'PySide.QtCore.Signal' object has no attribute 'connect'

我应该如何为 QRunnable 对象实现自定义信号?

QRunnable 不是 QObject,因此它不能有信号,因此一个可能的解决方案是创建一个提供信号的 class:

class FooConnection(QtCore.QObject):
    foosignal = QtCore.Signal(foo_type)

class Foo2(QtCore.QRunnable):
    def __init__(self):
        super(Foo2, self).__init__() 
        self.obj_connection = FooConnection()

    def run(self):
        # do something
        foo_value = some_operation()
        self.obj_connection.foosignal.emit(foo_value)

class Foo1(QtCore.QObject):
    def __init__():
        super().__init__()
        self.pool = Foo2()
        self.pool.obj_connection.foosignal.connect(foo)
        QtCore.QThreadPool.globalInstance().start(self.pool)

    @QtCore.Slot(foo_type)
    def foo(self, foo_value):
        # do something