I want to measure something continuously with a powermeter, and display some things in a GUI. I'm trying to setup a threading Worker class where the measure would happen and emit the signals with the results to the GUI. I've been looking at a lot of others posts (this, this, also this) but I'm not able to apply the solutions to my problem.
Here is a bit of the code :
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import Qt, QThread, pyqtSignal, QObject
class MySignal(QObject):
sig = pyqtSignal(str)
class Pyro(QtWidgets.QMainWindow):
def __init__(self):
super(Pyro, self).__init__()
#Interface setup
self.ui.stopButton.clicked.connect(self.stop)
#For the sample measurement
self.thread = Worker() #Creating a thread
#Connecting each signal to its label, so we can display the values
self.thread.power.sig.connect(self.setPower)
self.thread.residuals.sig.connect(self.setResiduals)
def stop(self):
""" stops the continuous mode"""
self.thread.running = False
def setPower(self, val): #functions to set values in the GUI
self.ui.valPowS.setText(val)
def setResiduals(self, val):
self.ui.valResiduals.setText(val)
class Worker(QThread):
def __init__(self):
QThread.__init__(self)
self.running = False #Helps to exit the thread
self.power = MySignal() #Creating signals to communicate with the GUI through the thread
self.residuals = MySignal()
self.power_sample_r = []
def sampleMain(self):
"""reads the power, do stuff"""
#Sending signals to the GUI to display them
self.power.sig.emit(str(self.power_sample_r))
self.residuals.sig.emit(str(self.residuals))
def contModeMeasure(self):
"""Last part of the sample measurement function where we call the thread and keep the measurement loop"""
self.display=False
while self.running: #If we haven't pressed the stop button, the measurement keeps going
self.sampleMain()
The GUI freezes forever, as it's in a loop, but I'm not so sure how to / when to send a signal to the GUI to display my results.
Any help would be appreciated, thanks a lot!
Edit : forgot the stopContMode, and bad indentation