How to connect pyqtSignal between classes in PyQT

前端 未结 1 804
囚心锁ツ
囚心锁ツ 2020-12-30 03:21

How to connect pyqtSignal between two different objects (classes) PROPERLY? I mean best practice.

Look what I have done to achieve the goal: The T

相关标签:
1条回答
  • 2020-12-30 03:37
    from PyQt4 import QtCore
    
    class Pot(QtCore.QObject):
    
        temperatureRaisedSignal = QtCore.pyqtSignal()
    
        def __init__(self, parent=None):
            QtCore.QObject.__init__(self)
            self.temperature = 1
    
        def Boil(self):
            self.temperatureRaisedSignal.emit()
            self.temperature += 1
    
    class Thermometer():
        def __init__(self, pot):
            self.pot = pot
            self.pot.temperatureRaisedSignal.connect(self.temperatureWarning)
    
        def StartMeasure(self):
            self.pot.Boil()
    
        def temperatureWarning(self):
            print("Too high temperature!")
    
    if __name__ == '__main__':
        pot = Pot()
        th = Thermometer(pot)
        th.StartMeasure()
    

    This is how I would've done it according to the docs:
    http://www.riverbankcomputing.com/static/Docs/PyQt4/html/new_style_signals_slots.html

    0 讨论(0)
提交回复
热议问题