Meloun Meloun - 9 months ago 73
Python Question

PyQt - slots with parameter


I have three pushbuttons, all makes almost the same.
I want to have only 1 slot-function for all 3 buttons.

def slotButtons(nr_button):

#common part

#for button 1
else if(nr==2):
#for button 2
else if(nr==3):
#for button 3

#common part

So I need something like slots with parameter..

QtCore.QObject.connect(pushButton1, QtCore.SIGNAL("clicked()"), slotButtons(1))
QtCore.QObject.connect(pushButton2, QtCore.SIGNAL("clicked()"), slotButtons(2))
QtCore.QObject.connect(pushButton3, QtCore.SIGNAL("clicked()"), slotButtons(3))

Can Python(pyQt) do something that?


What connect needs is any callable Python object. Since Python has functions as first-class objects, this is easy to implement with a wrapper function. For simple cases, a lambda would do:

    self.connect(pyuic4Button, SIGNAL("clicked()"),
            lambda: self.setPath("pyuic4"))
    self.connect(pyrcc4Button, SIGNAL("clicked()"),
            lambda: self.setPath("pyrcc4"))