作为 QThread

问题描述

我有一项繁重的任务,每 500 毫秒就会持续运行一次。它包括更新 GUI 元素,我需要随时访问其变量。

执行的任务:一个动态更新的列表,每 500 毫秒,循环遍历该列表,并在其中包含的元素上执行任务。有时我没有元素,有时我有很多。

加载列表时,用户开始遇到鼠标移动、按键等延迟。毫无疑问,这是因为每 500 毫秒执行一次繁重的任务。

有没有办法让我将这个 QTimer 任务放入 QThread 并不断访问它的元素以更新其中包含的列表?

换句话说,我希望它始终在后台运行,但也能够在任何给定时刻更新其中使用的列表。

我正在使用 PySide2;我看过一些例子,但没有一个适合我想要完成的任务。

示例: 我想根据需要从主线程更新“aList”元素。如果列表为空,则 for 循环不执行任何操作。否则,它会遍历元素并给它们加 1。

“run”函数应该设置一个 500ms 的 Qtimer。

有时列表可能是空的,有时会充满元素。它的大小由 GUI 线程控制。

 from PySide2 import QtCore
 from PySide2 import QtGui 
 from PySide2 import QtWidgets

 import sys
 import time

 class RxProcess(QtCore.QThread):

     output = QtCore.Signal()

     def __init__(self,parent = None):
         super(RxProcess,self).__init__(parent)
         self.aList = list()

    
     def run(self):
    
         # Loop through list
         for element in self.aList: 
        
             element += 1

             # Print to the gui the element that was just updated in the list
             self.output.emit(element)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)