I have created the following toy class with help from 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_()
How can I start the timer from a new thread when I use QThreadPool
?
I need to update the GUI repeatedly at regular intervals but if I add the QTimer
in the main thread the whole application feels really sluggish. My understanding is that by including this in a separate thread through QThreadPool
it may be a more efficient solution as the new thread can be self deleted automatically once it is done.
However, whenever I change QtCore.QThread
to QtCore.QRunnable
in the above class and I try to start the thread using the code below I get an error:
self.threadpool = QtCore.QThreadPool()
worker = Worker()
self.threadpool.start(worker)