Worker class used in GUI is nolonger compatible to Qtimer
-
While I'm trying to make a simple communication between the Worker thread and GUI, MainWindow to plot realtime data using photograph, accordingly my following code should receive rand int passed by the Worker to the MainWindow:
from PyQt5.QtCore import QThread, QObject, pyqtSignal, pyqtSlot from PyQt5 import QtWidgets, QtCore from pyqtgraph import PlotWidget, plot import pyqtgraph as pg import sys import os from random import randint class Worker(QObject): finished = pyqtSignal() intReady = pyqtSignal(int, name="intReady") def __init__(self): super().__init__() self.randNum = 0 @pyqtSlot() def procCounter(self): # A slot takes no params self.randNum = int(randint(0, 50)) # Add a new random value. self.intReady.emit(self.randNum) print("randNum:", self.randNum) self.finished.emit() class MainWindow(QtWidgets.QMainWindow): def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.newData = 0 self.obj = Worker() # no parent! self.thread = QThread() # no parent! self.obj.moveToThread(self.thread) self.obj.intReady.connect(self.getSignal) self.obj.finished.connect(self.thread.quit) self.thread.started.connect(self.obj.procCounter) self.thread.start() self.graphWidget = pg.PlotWidget() self.setCentralWidget(self.graphWidget) self.y = [randint(0, 50) for _ in range(50)] self.data_line = self.graphWidget.plot(self.y) self.timer = QtCore.QTimer() self.timer.setInterval(1000) #self.timer.timeout.connect(self.getSignal)# Here is my problem #self.timer.timeout.connect(lambda g=self.newData: self.update_plot_data(g)) self.timer.timeout.connect(self.update_plot_data) self.timer.start() @pyqtSlot(int) def getSignal(self, i): self.newData = i # get a new random value. print("getSignal:",i) def update_plot_data(self): self.y = self.y[1:] # Remove the first print("update_plot_data:",self.newData) self.y.append(self.newData) # Add a new random value. self.data_line.setData(self.y) # Update the data. worker = Worker() app = QtWidgets.QApplication(sys.argv) w = MainWindow() w.show() sys.exit(app.exec_())
My problem is about: 1.not updating the plot by rand int emitted signal. 2. As can be seen, the Qtimer calling is faster than the rand int emitting . Here self.thread.start() is never called.
The issue seems to be managing compatibility between Qtimer and @pyqtSlot(). This way self.thread.start() would start. Is there any point to get it to work?? -
Hi and welcome to devnet,
Which version of PyQt5 are you using ?
Which version of Python ?
On which OS ?
How did you install it ?
What output are you getting from your application ?
Do you have any warning / message on the command line ? -
@ocien
While you wait for someone else to answer who perhaps understands what you are saying/asking, I do not.As can be seen, the Qtimer calling is faster than the rand int emitting . Here self.thread.start() is never called.
I cannot "see" anything. You do not describe what does or does not happen when this code is run.
The issue seems to be managing compatibility between Qtimer and @pyqtSlot().
What does this mean, what "compatibility" between a
QTimer
signal and a slot?#self.timer.timeout.connect(self.getSignal)# Here is my problem #self.timer.timeout.connect(lambda g=self.newData: self.update_plot_data(g)) self.timer.timeout.connect(self.update_plot_data)
What "# Here is my problem"? What problem? Are some of these lines not supposed to be commented out?
Just one thought looking at your code. I don't know whether it is happening or whether it is relevant. If you have multiple slots connected to a signal (
timeout
here) and the connection is across threads (as it is here) my understanding is that the order such slots is executed is "undefined", it does not necessarily have to be the the order they areemit
ted. Is this relevant to whatever your situation is? -
Hi @JonB
Based on my code, the plot is totally keeping updated just by the first emitted, intReady signal!
While this is not my aim here. How it can be possible to make the intReady signal goes in the same order as the Qtimer??? This way real-time plotting would be accomplished.
I mean, trigger the Qtimer or anyway to update plot just when the new data is received. -
Are you asking that a signal coming from an independent thread comes in a deterministic way with a timer running in a different thread ?
-
@SGaist , Here I made some changes, so plot get updated but some errors also come too:
from PyQt5.QtCore import QThread, QObject, pyqtSignal, pyqtSlot #import time from PyQt5 import QtWidgets, QtCore, QtTest from pyqtgraph import PlotWidget, plot import pyqtgraph as pg import sys import os from random import randint class Worker(QObject): finished = pyqtSignal() intReady = pyqtSignal(int, name="intReady") def __init__(self): super().__init__() self.randNum = 0 @pyqtSlot() def procCounter(self): # #for i in range(1, 100): self.randNum = int(randint(0, 50)) self.intReady.emit(self.randNum) print("randNum:", self.randNum) timer.sleep(1000) #self.timer = QtCore.QTimer() #self.timer.sleep(1000) #QtTest.QTest.qWait(1) self.finished.emit() class MainWindow(QtWidgets.QMainWindow): def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.timer = QtCore.QTimer() self.timer.setInterval(1000) self.newData = 0 self.obj = Worker() # no parent! self.thread = QThread() # no parent! self.obj.moveToThread(self.thread) self.obj.intReady.connect(self.getSignal) self.obj.finished.connect(self.thread.quit) self.thread.started.connect(self.obj.procCounter) self.thread.start() self.graphWidget = pg.PlotWidget() self.setCentralWidget(self.graphWidget) self.y = [randint(0, 50) for _ in range(50)] self.data_line = self.graphWidget.plot(self.y) self.timer.timeout.connect(self.obj.procCounter) # recent change is here self.timer.timeout.connect(self.update_plot_data) self.timer.start() @pyqtSlot(int) def getSignal(self, i): self.newData = i # get a new random value. print("getSignal:",i) def update_plot_data(self): self.y = self.y[1:] # Remove the first print("update_plot_data:",self.newData) self.y.append(self.newData) # Add a new random value. self.data_line.setData(self.y) # Update the data. worker = Worker() app = QtWidgets.QApplication(sys.argv) w = MainWindow() w.show() sys.exit(app.exec_())
Traceback (most recent call last):
File "D:\AppZ\Pycharm2025Proj\QtPort\WorkerPyGraph_1.py", line 23, in procCounter
timer.sleep(1000)
^^^^^
NameError: name 'timer' is not defined. Did you mean: 'iter'How I could make a synchronized Qtimer and emitting recent data in a manageable way?
-
@ocien said in Worker class used in GUI is nolonger compatible to Qtimer:
NameError: name 'timer' is not defined. Did you mean: 'iter'
I'm not sure whether this is the best way to do whatever it is you want (no
qDebug()
s/print()
s to show what is arriving in what undesired order), but if you do want to use this it is purely Python. So you need toimport
whatever Python module gives you sometimer
object you can callsleep()
on, nothing to do with Qt.How I could make a synchronized Qtimer and emitting recent data in a manageable way?
"Synchronized" to what? And what is a "manageable way"? If you have data arriving or generated in a thread then either you can raise a signal immediately when you receive it (e.g. if you generate it or it's external and produces some kind of "interrupt" or "callback" when new data arrives) or you can have a timer which "polls" for new data intermittently. In either case you can either raise a signal for each new data point individually or you can accumulate/buffer new data into a list/array and emit a single signal with a number of new data points when a certain time has
pastpassed or the number of points exceeds some threshold. -
@JonB said in Worker class used in GUI is nolonger compatible to Qtimer:
@ocien said in Worker class used in GUI is nolonger compatible to Qtimer:
NameError: name 'timer' is not defined. Did you mean: 'iter'
I'm not sure whether this is the best way to do whatever it is you want (no
qDebug()
s/print()
s to show what is arriving in what undesired order), but if you do want to use this it is purely Python. So you need toimport
whatever Python module gives you sometimer
object you can callsleep()
on, nothing to do with Qt.How I could make a synchronized Qtimer and emitting recent data in a manageable way?
"Synchronized" to what? And what is a "manageable way"? If you have data arriving or generated in a thread then either you can raise a signal immediately when you receive it (e.g. if you generate it or it's external and produces some kind of "interrupt" or "callback" when new data arrives) or you can have a timer which "polls" for new data intermittently. In either case you can either raise a signal for each new data point individually or you can accumulate/buffer new data into a list/array and emit a single signal with a number of new data points when a certain time has past or the number of points exceeds some threshold.
@JonB, Exactly what I need. In the Worker thread new data should be accumulated and passed to the pyqtgraph. But I don't know how for a certain time or a defined number of data do that using signal/slot approach. Not to know whether use a "broadcast signal", using a single instance of the class for that signal and use it in any sender or receiver instance or just take another path to. Is it possible let me know using a short code?
-
@JonB The print statement has been.
To show again:getSignal: 46
update_plot_data: 46
randNum: 39
getSignal: 39
Traceback (most recent call last):
File "D:\AppZ\Pycharm2025Proj\QtPort\WorkerPyGraph_1.py", line 23, in procCounter
timer.sleep(1000)
^^^^^
NameError: name 'timer' is not defined. Did you mean: 'iter'? -
@ocien said in Worker class used in GUI is nolonger compatible to Qtimer:
NameError: name 'timer' is not defined. Did you mean: 'iter'?
Are you intending to fix this?
Other than that I don't see what is wrong with the output, perhaps you would care to explain?
-
@JonB No intending. When the interval for emitting Qtimer get triggered faster for example 1ms, then inconsistency becomes observable, as you can see:
randNum: 3
update_plot_data: 50
getSignal: 37
randNum: 22
getSignal: 3
update_plot_data: 3
getSignal: 22
randNum: 50
update_plot_data: 22
getSignal: 50
randNum: 39
update_plot_data: 50
getSignal: 39
randNum: 11
update_plot_data: 39
It seems to me this way of signal/slot coding to plot real-time data, is not a systematic or precise common way to do it. I need to make emitting number and plotting it in a consistent way. Do you have any suggestion to make things work in terms of signal/slot approach? Or just by data down sampling should I keep going? If you were in my shoes how did you do it? -
@ocien
Looking at your code, which has bits commented out, am I to understand you have one timer in the main thread ticking at around 1ms and a separate delay (somehow) of 1ms in a thread, they exchange information (a random number), and you think these two separate millisecond timers (plus any thread switching) should be so perfectly in sync that you can rely on one ticking (generate data) followed by the other (plot data) ticking?