【发布时间】:2020-05-26 15:45:56
【问题描述】:
我在this answer 的帮助下创建了以下玩具类:
class Worker(QtCore.QThread):
def work(self):
print("message")
def __init__(self):
super(Worker, self).__init__()
self.timer = QtCore.QTimer()
self.timer.moveToThread(self)
self.timer.timeout.connect(self.work)
def run(self):
self.timer.start(1000)
loop = QtCore.QEventLoop()
loop.exec_()
当我使用QThreadPool 时,如何从新线程启动计时器?
我需要定期重复更新 GUI,但如果我在主线程中添加 QTimer,整个应用程序会感觉非常缓慢。我的理解是,通过 QThreadPool 将其包含在单独的线程中,这可能是一个更有效的解决方案,因为新线程可以在完成后自动自行删除。
但是,每当我在上面的类中将 QtCore.QThread 更改为 QtCore.QRunnable 并尝试使用下面的代码启动线程时,我都会收到错误:
self.threadpool = QtCore.QThreadPool()
worker = Worker()
self.threadpool.start(worker)
【问题讨论】:
标签: python python-3.x pyqt5 pyside2