Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

qt

pyqt

Notice the line redb.clicked[bool].connect(self.setColor), why did it add the part [bool]? I tried to remove the part, modified 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()    
like image 737
Searene Avatar asked Jan 09 '23 15:01

Searene


1 Answers

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

If a signal is not overloaded you don't need to do it, and indexing with the signature has the same effect as no index at all.

Example:

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_()

Sample output:

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'>

Now you rightfully ask How does PyQt choose the default overload? (the third connect() in the example). The answer is: they document this in the PyQt docs. One of the few differences between the C++ Qt docs and the PyQt docs. For example: http://pyqt.sourceforge.net/Docs/PyQt4/qspinbox.html#valueChanged . It seems to me that the default signal is always the one that is declared first in the C++ header, but this might not always be the case.

A signal may be overloaded, ie. a signal with a particular name may support more than one signature. A signal may be indexed with a signature in order to select the one required. A signature is a sequence of types. A type is either a Python type object or a string that is the name of a C++ type. The name of a C++ type is automatically normalised so that, for example, QString can be used instead of the non-normalised const QString &.

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

like image 136
dom0 Avatar answered Jan 17 '23 15:01

dom0