'PySide2.QtCore.Signal'对象没有属性'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 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

相关内容

  • 没有找到相关文章

最新更新