本文介绍了从QRunnable发出信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从QRunnable向我的主QObject发送信号,但是由于某种原因,它没有接收到它们.

I'm trying to send a signal from a QRunnable to my main QObject, but for some reason it isn't receiving them.

这是正确的方法吗?

这是一个小测试用例:

import sys

from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QThreadPool, QObject, QRunnable, pyqtSignal

class WorkerSignals(QObject):
    result = pyqtSignal(int)

class Worker(QRunnable):
    def __init__(self, task):
        super(Worker, self).__init__()

        self.task = task
        self.signals = WorkerSignals()

    def run(self):
        print 'Sending', self.task
        self.signals.result.emit(self.task)

class Tasks(QObject):
    def __init__(self):
        super(Tasks, self).__init__()

        self.pool = QThreadPool()
        self.pool.setMaxThreadCount(1)

    def process_result(self, task):
        print 'Receiving', task    # This does not run

    def start(self):
        for task in range(10):
            worker = Worker(task)
            worker.signals.result.connect(self.process_result)

            self.pool.start(worker)

        self.pool.waitForDone()

if __name__ == '__main__':
    app = QApplication(sys.argv)

    stuff = Tasks()
    stuff.start()

推荐答案

您需要致电app.exec_()

检出 PyQt4中的事件和信号

这篇关于从QRunnable发出信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 07:27