我试图找出子类QtConcurrent并在其中编写运行方法是否有效:
class Task(QtCore.QtConcurrent):
def run(self, function):
function()还是完全没用?
发布于 2015-09-03 15:52:50
这是完全无用的,因为QtConcurrent是一个名称空间,而不是类。
而且,PyQt和PySide都不提供QtConcurrent提供的任何功能,因为它都是基于模板的,因此不可能包装。
PS:您链接到的PySide文档用于ReduceOption枚举。由于怀疑该枚举在QtConcurrent命名空间之外是否有任何用途,所以PySide可能包含了一个bug。
发布于 2018-04-24 07:26:03
您要找的类是QRunnable。
发布于 2019-03-04 22:23:57
在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允许嵌套函数和相互之间的类!
https://stackoverflow.com/questions/32378719
复制相似问题