I’m stuck with the following problem. I’m trying to connect a lambda function to a Signal for passing some extra data eventually.
def createTimeComboBox(self,slotCopy):
timeComboBox = QComboBox()
#...
cmd = lambda func=self.test:func()
self.connect(timeComboBox, SIGNAL("currentIndexChanged(int)"),cmd)
#...
def test(self, value):
print value
When I run createTimeComboBox(), I get this error:
TypeError: 'int' object is not callable
Changing
self.connect(timeComboBox, SIGNAL("currentIndexChanged(int)"),cmd)
to
self.connect(timeComboBox, SIGNAL("currentIndexChanged(int)"),self.test)
works fine, but I would like to be able to pass the slotCopy variable as well, so assumed that I need to use the lambda approach.
I had done this previously with a QPushButton‘s clicked() signal and that worked fine.
def createToDoctorButton(self,extraData):
toDoctorButton = QPushButton()
cmd = lambda func=self.goToDoctor:func(extraData)
self.connect(toDoctorButton, SIGNAL('clicked()'),cmd)
return toDoctorButton
def goToDoctor(self,extraData):
print extraData
I hope this makes sense – does anyone have any ideas? Thanks for any suggestions!
Cheers
Dave
Your
lambdaaccepts a parameter (func):Although the parameter has a default, it will be replaced if a parameter is passed. Looking at the signal,
currentIndexChanged(int), shows that the signal will pass an integer parameter.funcwill be the integer coming fromcurrentIndexChanged. Later, doingfunc()will be effectively trying to call an integer object which obviously is not legal (as the error indicates)You need another parameter in your
lambdato “catch” the passed parameter without overriding thefuncparameter:By the way, your
testmethod expects a parameter, so you can’t just dofunc().You didn’t have that problem with the
clicked()signal because it doesn’t pass a parameter to replace the default value.