Connect QRunnable to a function/method when finished

前提是你 提交于 2020-02-04 04:51:17

问题


QThread has a finished signal, and I am able to do something when thread is finished (connect to a method/function), however I would like to do this with QRunnable as well. Is there a way to connect the QRunnable thread to a method/function when it is done?

QThread:

class HelloWorldTask(QThread):
    def __init__(self):
        QThread.__init__(self)
    def run(self):
        import time
        time.sleep(3)
        print ("Running thread \n")
        time.sleep(3)

hello.finished.connect(check)

def check():
    print('Thread Done')

output:

Running thread

finished

QRunnable:

instance = QThreadPool.globalInstance()


class HelloWorldTask(QRunnable):
    def __init__(self):
        super().__init__(self)
    def run(self):
        import time
        time.sleep(3)
        print ("Running thread \n")
        time.sleep(3)

hello = HelloWorldTask()
#hello.finished.connect(check) <-- how to connect to a method/function when finished.
instance.start(hello)
print(instance.waitForDone())

def check():
    print('Thread Done')

desired output:

Running thread

Thread Done


回答1:


QRunnable does not have a finished signal since it is not a QObject, so a possible solution is to create another class that inherits from QObject so that it emits the signal:

from PyQt5 import QtCore


class Signals(QtCore.QObject):
    finished = QtCore.pyqtSignal()


class HelloWorldTask(QtCore.QRunnable):
    def __init__(self):
        super().__init__()
        self.signal = Signals()

    def run(self):
        import time

        time.sleep(3)
        print("Running thread \n")
        time.sleep(3)
        self.signal.finished.emit()


def check():
    print("Thread Done")
    QtCore.QCoreApplication.quit()


if __name__ == "__main__":
    import sys

    app = QtCore.QCoreApplication(sys.argv)
    hello = HelloWorldTask()
    hello.signal.finished.connect(check)
    QtCore.QThreadPool.globalInstance().start(hello)
    sys.exit(app.exec_())


来源:https://stackoverflow.com/questions/56811721/connect-qrunnable-to-a-function-method-when-finished

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!