Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

class HMI to another class how make the dialog?



  • Hello,
    I am working on a graphical interface which uses the HMI class to control a widget lcdNumber.
    in another class i need to modify the value of lcdNumber but as I am no longer in the same class, the widget is no longer accessible and I have a message "self is not define).

    i don't know how can i do this ? (i just put a piece of code i didn't put all the variables and configuration so you can see more clearly)
    Thank you for your help

    code_text
    ```class HMI(QDialog):
        def __init__(self):
            super(HMI, self).__init__()
            loadUi('BAI.ui',self)
            self.checkBox.stateChanged.connect(self.Alstom2570) #Programme Alstom -25°C à 70°C valider appel de la fonction Alstom2570
            self.spinBox.valueChanged.connect(self.etuve)
            self.lcdNumber.display(count)    
            #stateChanged
    
    class Pi_hat_adc:
        def __init__(self,bus_num=1,addr=ADC_DEFAULT_IIC_ADDR):
            self.bus=Bus(bus_num)
            self.addr=addr
            print("adc")
    def Alstom2570class():
                   global count
                   while T < SB :  #tant quon est a froid (en dessous du seuil bas
            
                    GPIO.output(16, GPIO.HIGH)      #met la pin 16 à 1 (GPIO23)
            
                    count=count+1 #incrémentation de la variable count pour compter les démarrages
            
                    self.lcdNumber.display(count)
            
                    QtTest.QTest.qWait((TonF)) # on bloque la sortie GPIO a 1 pendant 15 min

  • Lifetime Qt Champion

    Hi,

    You can use signals and slots to communicate properly between your classes.



  • @SGaist I manage very well to do this in the same class on the other hand when it is necessary to pass from one class to another I do not know how to do it. do you have an example so i can understand? thank you



  • @ZZR69
    You just need instances of the two classes you want to connect. Then:

    signalling_instance.signal_name.connect(slot_instance.slot_name)
    


  • Thanks a lot,
    for exemple in my code if i run like that i have error message on line :

    code_text
    ``` self.lcdNumber.display(count)" self is not defined.
    

    So i have try to replace this ligne by :

    self.HMI.lcdNumber.connect(self.display(count))
    and new error is : self.HMI.lcdNumber.connect(self.display(count))
    AttributeError: 'Pi_hat_adc' object has no attribute 'HMI'

    in fact i try to write the "count" variable of lcdNumber widget where is in class HMI


  • Lifetime Qt Champion

    @ZZR69 said in class HMI to another class how make the dialog?:

    self.lcdNumber.display(count)" self is not defined.

    Well, you seem to call this in a function which is not a method. Please show the code where you call this...



  • thanks , here you are the start code with HMI class where i write 0 in lcdNumber :

    class HMI(QDialog):
        def __init__(self):
            super(HMI, self).__init__()
            loadUi('BAI.ui',self)
            self.checkBox.stateChanged.connect(self.Alstom2570) #Programme Alstom -25°C à 70°C valider appel de la fonction Alstom2570
            self.spinBox.valueChanged.connect(self.etuve)
            self.lcdNumber.display(count)    
            #stateChanged
    

    After i have create a function called alstom2570()

    def Alstom2570(self): #Fonction appele lors de la selection du programme Alstom -25°C à 70°C
            #Toutes les variables sont declare et initialise pour correspondre au programme T2
            print("case coche")#debug
            global SB #definition de la variable SB (temperature seuil bas mise ss tension)
            global SH #definition de la variable SH (temperature seuil haut mise ss tension)
            SB=600 #variable definissant le seuil de temperature basse (si la temperature etuve descend sous -20°C mise ss tension)-20°C=600mV
            SH=3001 #variable definissant le seuil de temperature haute (si la temperature etuve depasse 65°C mise ss tension) 65°C=3001mV
            ####on choisis -20 et 65 afin d etre sur que l etuve arrive a ces temperature et quil n'y ai pas doscillation de temperature
            ###ayant pour effet des demarrages alleatoirs
            global TonF
            TonF=15*60000 #Temps ON 15min en ms (mise ss tension) a froid (seuil bas)
            global ToffF
            ToffF=15*60000 #Temps OFF 15min (mise hors tension) à froid (seuil bas)
            global T
            #T=-25 #uniquement pour debug ensuite T sera la value par ladc
            print("appel programme alstom2570")
            
            Alstom2570class() #appel de la fonction permettant de sortir de class interface graphique Qt
            
    

    in this function i call anoter function named Alstom2570class and here i need to write in lcdNumber :

    self.lcdNumber.display(count)
    
    

    but i can't acces to lcdNumber in this function...


  • Lifetime Qt Champion

    If that:
    @ZZR69 said in class HMI to another class how make the dialog?:

    Alstom2570class() #appel de la fonction permettant de sortir de class interface graphique Qt

    refers to:
    @ZZR69 said in class HMI to another class how make the dialog?:

    def Alstom2570class()

    then you have an issue in your understanding of how Python classes work as well as the signals and slots concept of Qt.

    I would recommend to take a step back and start with a minimal application showing a QSpinBox and a QLabel. Then connect the spin box to a slot that will update the QLabel with its value. Once you have that working, you'll be able to implement your application.


Log in to reply