I have a QTableView
to which I want to set a QPushButton
for every row. I am doing this as follows within my class derived from QWidget
following an example found here:
for index in range(number_rows):
btn_sell = QPushButton("Edit", self)
btn_sell.clicked.connect(self.button_edit)
table_view.setIndexWidget(table_view.model().index(index, 4), btn_sell)
If the table is drawn and I click on one of the QPushButton
the method self.button_edit
is called - but which one? It does not seem that an 'event' of any sort is given to self.button_edit
, so how can I find the row-index of the QPushButton
that was clicked within the button_edit
method?
Maybe there is a different way altogether to add a button to each row of a table?
Your event handler will look similar to this:
This uses the
indexAt
function to get the button's position.For clarity, my script looks like this:
Which will produce a small GUI like this:
When the
Edit
buttons are clicked, it prints, to the console:The first element is your row index, the second is your column (remember it is 0 based, which is why it shows 2, not 3 - despite the column headers).