0

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

gwaedon
  • 13
  • 3
  • Where is the code of `stopContMode`? Also, QThread inherits from QObject, so there's absolutely no need to create another instance for its signals, just add them to the `Worker` class instead. – musicamante May 05 '22 at 12:43
  • Yep, I forgot that part, my bad! Are you talking about the 'MySignal()' class ? I'm not sure I understand where I'm mistaken sorry – gwaedon May 05 '22 at 12:59
  • Please provide a *full* [mre], as right now your code does absolutely *nothing*. You don't need the `MySignal` class: add two separate signals to the `Worker` class and use them instead. – musicamante May 05 '22 at 13:04

0 Answers0