0

In PyQt5 I generate as many QPushButtons as there are items in a list, like:

self.modelsButton = [QtWidgets.QPushButton(self.resultsWidget) for x in modelsDefined]

And then I make them execute a function when clicked:

[self.ui.modelsButton[i].clicked.connect(self.populateDetailsTab) for i in range(len(self.modelsDefined))]

The function populateDetailsTab will attempt to pull the text details of the button clicked, but I can't decipher which button was clicked. I tried to pull the text of the button directly via the following:

[self.ui.modelsButton[i].clicked.connect(self.populateDetailsTab(self.ui.modelsButton[i].text().split[0])) for i in range(len(self.modelsDefined))]

But this throws an error: TypeError: 'builtin_function_or_method' object is not subscriptable.

Can someone please help me identify which button was clicked? There are a decent number of buttons generated in my script.

K-83Rick
  • 123
  • 2
  • 9
  • You could try using `self.sender()`. If called within a slot it returns the object that sent the signal that triggered the slot. – Heike Apr 25 '20 at 21:08
  • @Heike this worked! I wasn't aware of this before, but I found an old [thread](https://stackoverflow.com/questions/13050810/pyqt-button-clicked-name) that answered this question already. Thanks for drawing my attention to this. – K-83Rick Apr 25 '20 at 21:57
  • use `[button.clicked.connect(lambda *args, text=button.text() :self.populateDetailsTab(text) for button in self.modelsDefined]` – eyllanesc Apr 25 '20 at 22:54

0 Answers0