PyQt4 signals and slots

后端 未结 7 2029
执笔经年
执笔经年 2021-01-31 06:40

I am writing my first Python app with PyQt4. I have a MainWindow and a Dialog class, which is a part of MainWindow class:

self.loginDialog = LoginDialog();
         


        
7条回答
  •  Happy的楠姐
    2021-01-31 07:09

    There are some concepts to be clarified

    [QT signal & slot] VS [Python signal & slot]

    All the predefined signals & slots provided by pyqt are implemented by QT's c++ code. Whenever you want to have a customized signal & slot in Python, it is a python signal & slot. Hence there are four cases to emits a signal to a slot:

    • from a QT signal to a QT slot
    • from a QT signal to a Python slot
    • from a Python signal to a QT slot
    • from a Python signal to a Python slot

    The code below shows how to connect for these four different scnarios

        import sys
        from PyQt4.QtCore import *
        from PyQt4.QtGui import *
    
        class Foo(QtCore.QObject):
    
            def __init__(self, parent=None):
                super(Foo, self).__init__(parent)
                dial = QDial()
                self.spinbox = QSpinbox()
    
                # --------------------------------------
                # QT signal & QT slot
                # --------------------------------------
    
                # option 1: more efficient 
                self.connect(self.spinbox, SIGNAL("valueChanged(int)"), 
                    dial, SLOT("setValue(int)"))
                # option 2:
                self.connect(self.spinbox, SIGNAL("valueChanged(int)"), 
                    dial.setValue)
    
    
                # --------------------------------------
                # QT signal & Python slot
                # --------------------------------------
    
                self.connect(self.spinbox, SIGNAL("valueChanged(int)"), 
                    self.myValChanged)
    
    
                # --------------------------------------
                # Python signal & Qt slot
                # --------------------------------------
    
                # connect option 1: more efficient
                self.connect(self, SIGNAL("mysignal"), dial, 
                    SLOT("setValue(int)"))
    
                # connect option 2:
                self.connect(self, SIGNAL("mysignal"), dial.setValue)
    
                # emit
                param = 100
                self.emit(SIGNAL("mysignal"), param)
    
    
                # --------------------------------------
                # Python signal & Python slot
                # --------------------------------------
    
                # connect
                self.connect(self, SIGNAL("mysignal"), self.myValChanged)
    
                # emit
                param = 100
                self.emit(SIGNAL("mysignal"), param)
    
    
        def myValChanged(self):
            print "New spin val entered {0}".format(self.spinbox.value())
    

    Conclusion is --

    Signal signature for Python signal differentiate from that of QT signal in that it doesn't have the parenthesis and can be passed any python data types when you emit it. The Python signal is created when you emit it.

    For slot, there are three forms of signatures.

    • s.connect(w, SIGNAL("signalSignature"), functionName)
    • s.connect(w,SIGNAL("signalSignature"), instance.methodName)
    • s.connect(w,SIGNAL("signalSignature"), instance, SLOT("slotSignature"))

    Number 1 & 2 are available for Python slot, while number 2 & 3 are available for QT slot. It is clear that besides QT predefined slot, any python callable function/methods is qulified to be a Python slot.

    These points are made in Summerfield's article on Signals and Slots.

    [Old style qt signal & slot] VS [new style qt singal & slot]

    Well, all the description above is based on the old style pyqt signal & slot. As @Idan K suggested there is an alternative new-style to do the things, especially for the Python signal. Refer to here for more.

提交回复
热议问题