PyQt sending parameter to slot when connecting to a signal PyQt sending parameter to slot when connecting to a signal python python

PyQt sending parameter to slot when connecting to a signal


Use a lambda

Here's an example from the PyQt book:

self.connect(button3, SIGNAL("clicked()"),    lambda who="Three": self.anyButton(who))

By the way, you can also use functools.partial, but I find the lambda method simpler and clearer.


As already mentioned here you can use the lambda function to pass extra arguments to the method you want to execute.

In this example you can pass a string obj to the function AddControl() invoked when the button is pressed.

# Create the build button with its captionself.build_button = QPushButton('&Build Greeting', self)# Connect the button's clicked signal to AddControlself.build_button.clicked.connect(lambda: self.AddControl('fooData'))def AddControl(self, name):    print name

Source: snip2code - Using Lambda Function To Pass Extra Argument in PyQt4


use functools.partial

otherwise you will find you cannot pass arguments dynamically when script is running, if you use lambda.