Python: Unable to define pyqtSignal correctly in QThread

Viewed 39

I need to perform long-run calculations so I would like to use threading and Pyqt signal/slot to send data between threads. I already managed few years ago to do so, but for a few days now, I cannot find why it is not working this time. I have read many posts on the web but none has yet succeed to help me find where I did something wrong. I voluntarily simplified my code so I could submit it to your help: Slot get_data in class RunOffsetCalc does never receive the text emit by signal corr_ray_data. I have tried many differents code structure. All seemed to have the same issue about the connection signal/slot. Even pycharm (v2022.2.1) highlight me warnings about not finding reference connect and emit in pyqtSignal | pyqtsignal... What am I doing wrong?

import sys
from time import sleep
from PyQt5.QtCore import QCoreApplication, QThread, pyqtSignal, pyqtSlot
    

class Main(QCoreApplication):
    """Main app to start calculation process"""

    def __init__(self, args):
        super(self.__class__, self).__init__(args)

        # thread palier
        self.thd_offset = RunOffsetCalc()
        self.thd_palier = RunStageCalculation()
        self.thd_palier.corr_ray_data.connect(self.thd_offset.get_data)

        # lancement des threads
        # on lance d abord les thread qui recevront les donnees pour le calcul des valeurs OFFSET
        self.thd_offset.start()
        sleep(1)
        self.thd_palier.start()
        # on lance ensuite le traitement de base pour chaque palier
        while not self.thd_palier.isRunning():
            sleep(.1)
        print("thd_palier demarre")
        self.thd_offset.wait()
        self.quit()


class RunStageCalculation(QThread):
    """Thread to run calculation for one stage to get averaged corrected radiation values
    for the stage"""

    # [av_ray_vect_corr]
    corr_ray_data = pyqtSignal(str)

    def __init__(self):
        super(self.__class__, self).__init__()

    def run(self):
        print("Execution des calculs pour le palier")
        self.corr_ray_data.emit('test')
        print("Fin d execution des calculs du palier")


class RunOffsetCalc(QThread):
    """Thread to calculate offset giving the corresponding stages and radiation kind"""

    def __init__(self):
        super(self.__class__, self).__init__()
        self.result = None

    @pyqtSlot(str)
    def get_data(self, strg):
        self.result = strg
        print(f"getdata declenchee: {self.result}")

    def run(self):
        print("Lancement RunOffsetCalc")
        while not self.result:
            sleep(1)
        print("Fin d execution RunOffsetCalc")


main = Main(sys.argv)
main.app.exec_()
0 Answers
Related