[PyQt] QPushButton.clicked() methods in PyQt5

David Cortesi davecortesi at gmail.com
Sat Aug 30 00:58:44 BST 2014


Working code in PyQt4, to connect signals from an array of QPushButtons:

    for j in range(number_of_buttons):
        self.connect(self.userButtons[j], SIGNAL("clicked()"),
                                lambda b=j : self.user_button_click(b) )

This caused the clicked() signal of button j to invoke user_button_click()
passing the value of j, i.e. the index to the clicked button.

Moving this code to the PyQt5 signal API:

    for j in range(number_of_buttons):
        self.user_buttons[j].clicked.connect(
                lambda b=j : self.user_button_click(b)
                )

The result is not as before. The user_button_click() method receives False
from any button, not an int.

At first I thought -- oh, the signal is defined as "clicked(bool
checked=False)" and so the checked property value is being passed. Even
though the button is (by default) not checkable, somehow I am getting the
clicked(bool) version instead of the clicked() overload.

But on second thought -- how could the signal's parameter get into the
lambda expression, which made no reference to it?

If that is the case, what would be the modern API's version of
SIGNAL('clicked()'), i.e. selecting a version of a signal that passes NO
parameter?

Thanks for your thoughts,
Dave Cortesi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20140829/17cd9a7c/attachment.html>


More information about the PyQt mailing list