使用current.futures.ThreadPoolExecutor时,PyQt5小部件Qthread问题

问题描述

我一直试图使用concurrent.futures.ThreadPoolExecutor()在我的应用程序中运行一些后台任务,以便在这些任务(“测量”)运行时能够与GUI交互。完成这些任务后,我将分配一个回调函数来更新GUI的某些字段,然后尝试根据这些字段来更新GUI小部件(图形,表格,列表等)。

这里是一个例子:

class MainWindow(QtWidgets.QMainWindow):
    
    def __init__(self):
        super(MainWindow,self).__init__()
        *some more code goes here*
        self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)

    def perform_measurement():
        future = self.executor.submit(*a function*)
        future.add_done_callback(self.update_gui_fields)

    def update_gui_fields(self,future):
        data = future.result()
        self.items_for_list.append(QStandardItem(data['key']))
        *more fields are updated here*

        self.QListView1.setModel(self.items_for_list)
        *more widgets are updated here*

问题在于字段正常更新,但是当我尝试与小部件交互时,应用程序崩溃。这是因为子级(此处为self.items_for_list)与父级(此处为self.QListView1)处于不同的线程中。这是我得到的错误:

QObject: Cannot create children for a parent that is in a different thread.
(Parent is QListView(0x555795efbc10),parent's thread is QThread(0x555795296600),current thread is QThread(0x7fd12400a100)
QBasicTimer::start: QBasicTimer can only be used with threads started with QThread

以前的帖子中找不到任何解决方案。关于如何攻击这个想法? 谢谢!

解决方法

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

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

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