PySide / PyQt中的QtConcurrent

问题描述 投票:4回答:3

我试图找出是否继承QtConcurrent并在其中编写run方法将起作用:

class Task(QtCore.QtConcurrent):

     def run(self, function):
           function()

还是完全没用?

python pyside qtconcurrent
3个回答
10
投票

它完全没用,因为QtConcurrent是一个命名空间,而不是一个类。

此外,PyQt和PySide都不提供QtConcurrent提供的任何功能,因为它都是基于模板的,因此无法包装。

PS:您链接的PySide文档是针对ReduceOption枚举的。由于令人怀疑这个枚举是否在QtConcurrent名称空间之外有任何用处,这可能是PySide包含它的一个错误。


0
投票

你要找的班级是QRunnable


0
投票

我在PyQt5中遇到了同样的问题。我想唯一的解决方案是在本地执行此操作:

def connect(self):
    class ConnectThread(QThread):
        def __init__(self, func):
            super().__init__()
            self.func = func
        def run(self):
            self.func()
    self.connectThread = ConnectThread(self._connect)
    self.connectThread.start()

def _connect(self):
    if self._driver is None:
        uri = self.uriString()
        if uri and self.user and self.password:
            self.statusMessage.emit("Connecting to the Graph Database....", -1, "color:blue;")
            try:
                self._driver = GraphDatabase.driver(uri, auth=(self.user, self.password))
                self.statusMessage.emit("Connected!", 5000, "color:green;")
            except Exception as e:
                self.clearStatusMessage.emit()
                Error(str(e)).exec_()
                if __debug__:
                    raise e

并记住将线程设置为成员变量:self.thread = ...或者您的线程引用将超出范围,并且很可能删除了线程对象。

您还可以将函数调用移动到它的本地定义中,因为Python允许嵌套函数和类彼此之间!

© www.soinside.com 2019 - 2024. All rights reserved.