Odd issue, thread worker & timmer not running properly
-
Hey
I've made this test >
from PySide6.QtCore import QObject, Signal, QTimer, QThread import sys from PySide6.QtWidgets import QWidget, QApplication class myWorker(QObject): triggerSignal = Signal() def __init__(self, threadName="newThread", threadInterval=1000): QObject.__init__(self) self.trigger = QTimer(self) # self.trigger.timeout.connect(self.triggerSignal) self.trigger.timeout.connect(self.ehh) self.trigger.setInterval(threadInterval) self.threadWorker = QThread() self.threadWorker.setObjectName(threadName) self.threadWorker.started.connect(self.startTimerAction) self.moveToThread(self.threadWorker) def ehh(self): print("gsdgsdf") def startTimerAction(self): print("5325235") self.trigger.start() print(self.trigger.isActive(), self.trigger.isSingleShot(), self.trigger.interval(), self.threadWorker.objectName(),QThread.currentThread(),self.thread()) def deleteWorker(self): self.trigger.stop() self.trigger = None self.threadWorker.quit() self.threadWorker.wait() self.threadWorker = None self.deleteLater() # self.deleteLater() # self.threadWorker.deleteLater() # self.threadWorker = None def startWorker(self, time=-1): if time != -1: self.trigger.setInterval(time) print("Starting worker?") self.threadWorker.start() a = QApplication() d = myWorker() d.startWorker() w = QWidget() w.show() sys.exit(a.exec_())
Which I'm using to create a Timmer that runs in other thread that calls a function. It allowes me to not have while loop, but a timmed function call latter in my app.
Strange thing, the exact same code in c++ works just fine, but in pyside it does not...
Any hints?TIA
-
@Dariusz said in Odd issue, thread worker & timmer not running properly:
Any hints?
You must say what exactly isn't running properly. From what I can tell the code is semantically correct (even though keeping the thread instance inside the worker object is very dubious design).
-
@kshegunov said in Odd issue, thread worker & timmer not running properly:
@Dariusz said in Odd issue, thread worker & timmer not running properly:
Any hints?
You must say what exactly isn't running properly. From what I can tell the code is semantically correct (even though keeping the thread instance inside the worker object is very dubious design).
Once running the app there should be
print("gsdgsdf")
every 1000 but it does not happen. The timmer does not expire/call a function from the thread. -
Hi,
No warning on the console ?
-
@jsulm said in Odd issue, thread worker & timmer not running properly:
@Dariusz Your QTimer is not created in the new thread. You should create it in startTimerAction to make sure it is created in the new thread.
Does it not get moved to new thread when its parent object gets moved to a new thread?
TIA
-
@kshegunov said in Odd issue, thread worker & timmer not running properly:
Does the timer start at all? Does the thread start at all?
the timer.start() gets called. But after that silence... Maybe everything gets deleted somewhere?