gpt4 book ai didi

python - 从 QRunnable 发出信号

转载 作者:太空宇宙 更新时间:2023-11-04 01:31:16 25 4
gpt4 key购买 nike

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

这样做正确吗?

这是一个小测试用例:

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

When we call the application's exec_() method, the application enters the main loop. The main loop fetches events and sends them to the objects. Signals and slots are used for communication between objects. A signal is emitted when a particular event occurs. A slot can be any Python callable. A slot is called when a signal connected to it is emitted.

结帐Events and Signals in PyQt4

关于python - 从 QRunnable 发出信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13786149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com