此类假设在单独的线程中处理长任务。
尽管将我的对象移动到新线程并使用排队连接,但信号是在主 gui 线程而不是
self.thread
处处理的。主 GUI 线程当然是调用 startit
的线程..
这是代码:
class DoLongProcess(QObject):
def __init__(self,task):
QObject.__init__(self)
self._task=task
self._realtask=None
self.thread = QThread()
self.moveToThread(self.thread)
self.thread.started.connect(self.run,Qt.QueuedConnection)
@Slot()
def run(self):
self._realtask()
def startit(self,*params):
self._realtask= partial(self._task,*params)
self.thread.start()
我使用pyside6。我尝试将
__init__
分成两种方法并从外部移动线程。还尝试不使用 QueuedConnection 属性(它应该准确地执行此操作)。这些都没有效果。
你知道为什么它不能按预期工作吗?
可能的解决方法之一是重新声明
thread.started
:
class QTracedThread(QThread):
# signal override is required here,
# or PyCharm users specifically during debug may face incorrect thread of slots connected to started
started_fix = Signal()
@override
def run(self):
# controls timer events tracing even when worker is used
pydevd.settrace(suspend=False)
self.started_fix.emit()
super().run()
# self.thread.started.connect(self.run, Qt.QueuedConnection)
self.thread.started_fix.connect(self.run, Qt.QueuedConnection)