Matthew Spydell
Matthew Spydell

Reputation: 53

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

I am trying to create a custom signal for a QRunnable Object for my PySide2 application. All examples have led me create a signal the following way:

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()

However, I am getting the following error on self.thread.signal.connect(foo):

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

How should I implement a custom signal for a QRunnable object?

Upvotes: 5

Views: 7508

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

A QRunnable is not a QObject so it can not have signals, so a possible solution is to create a class that provides the signals:

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

Upvotes: 10

Related Questions