I have a PushButton by the name of "Delete Row" with this coding,
self.deleteSectionButton = PySide2.QtWidgets.QPushButton("Delete Row")
self.deleteSectionButton.clicked.connect(self.on_section_table_click)
The function on_section_table_click basically has to delete the row clicked (it is not fully implemented but the idea is here and as you might see, I don't need different push buttons for each row to perform this functionality)
def on_section_table_click(self):
selected = self.ui.section_table.selectionModel().selectedIndexes()[0]
print(self.ui.section_table.item(selected.row(), 0).text())
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setText("Are you sure you want to delete the selected Section?")
msg.setStandardButtons(QMessageBox.Yes | QMessageBox.Cancel)
retval = msg.exec_()
And I am trying to add this push button in the last column of each row of my table. The code is as follows,
def get_section_data(self):
mycursor = self.DB.cursor()
Subquery = "Select * from tbl_section"
mycursor.execute(Subquery)
numcols = len(mycursor.fetchall()[0])
mycursor.execute(Subquery)
numrows = len(mycursor.fetchall())
self.ui.section_table.setRowCount(numrows)
self.ui.section_table.setColumnCount(numcols+1)
mycursor.execute(Subquery)
tablerow = 0
for row in mycursor.fetchall():
tablecol = 0
while tablecol < numcols:
self.ui.section_table.setItem(tablerow, tablecol, PySide2.QtWidgets.QTableWidgetItem(str(row[tablecol])))
tablecol += 1
self.ui.section_table.setCellWidget(tablerow, 2, self.deleteSectionButton)
tablerow += 1
However, it is only adding the Push Button in the last row of the table.
On trying different things, I found that Push Button is added to the last row to which it is being added, and it gets removed from the previous rows in which it was added earlier
For example, if I add this push button in first row and then on second row as well then it gets deleted from the first row.
However, I want this same Push Button on every row of the table. Can anyone tell how is it possible to add the same Push Button on every row of the table? I could make an array (of size = number of rows) of push buttons and then add them against their respective push buttons. But it will make the whole program a bit messy (considering that I also might have to make multiple functions against each Button or something like that)
Similarly, I can maybe make new Push Buttons inside the While loop and add them in the Row. But again, I wouldn't know how to associate function with them whenever they are clicked.