The difference between QtGui.QPushButton.clicked [bool] and QtGui.QPushButton.clicked

Pay attention to the line redb.clicked[bool].connect(self.setColor), why did she add a part [bool]? I tried to delete the part, changed the line to redb.clicked.connect(self.setColor), and the result was the same. So what is it?

import sys
from PyQt4 import QtGui

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()  
        self.initUI()

    def initUI(self):      

        self.col = QtGui.QColor(0, 0, 0)       
        redb = QtGui.QPushButton('Red', self)
        redb.setCheckable(True)
        redb.clicked[bool].connect(self.setColor)
        self.square = QtGui.QFrame(self)
        self.square.setGeometry(150, 20, 100, 100)
        self.square.setStyleSheet("QWidget { background-color: %s }" %  
            self.col.name())
        self.show()

    def setColor(self, pressed):  
        if pressed:
            val = 255
        else: val = 0
        self.col.setRed(v)    
        self.square.setStyleSheet("QFrame { background-color: %s }" %
            self.col.name())              

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()    
+4
source share
1 answer

In PyQt, you can index signals with their signature to select the correct congestion. (You read this right: you can overload Qt signals and provide different versions of the signal with completely different parameters). This is well supported by C ++, but overloads are not directly supported by Python, so this is an indexing mechanism.

, , , .

:

import sys
from PyQt4.QtGui import *
app = QApplication(sys.argv)

def valueChanged(eitherIntOrString):
    print(eitherIntOrString, type(eitherIntOrString))

spinbox = QSpinBox()
spinbox.valueChanged[str].connect(valueChanged)
spinbox.valueChanged[int].connect(valueChanged)
spinbox.valueChanged.connect(valueChanged)
spinbox.show()

app.exec_()

:

1 <class 'str'>
1 <class 'int'>
1 <class 'int'>
2 <class 'str'>
2 <class 'int'>
2 <class 'int'>
3 <class 'str'>
3 <class 'int'>
3 <class 'int'>

, PyQt ? ( connect() ). : PyQt. ++ Qt PyQt. : http://pyqt.sourceforge.net/Docs/PyQt4/qspinbox.html#valueChanged. , , ++, .

, .. . , . - . - Python, , ++. ++ , , , const QString &. QString.

: http://pyqt.sourceforge.net/Docs/PyQt4/new_style_signals_slots.html

+7

All Articles