0

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Sakib Khan
  • 305
  • 2
  • 13
  • 1
    You can't. Widgets are like physical objects, if you have 10 boxes and one shoe in the first box, if you put *that* shoe in the second box you're removing it from the first, and so on. If you need to show ten buttons, you need ten buttons. – musicamante Dec 04 '21 at 12:27
  • 1
    See: [How to select a QTableWidget cell containing a button](https://stackoverflow.com/q/35493642/984421). – ekhumoro Dec 04 '21 at 14:46
  • @musicamante That's a beautiful explanation! :| – Sakib Khan Dec 04 '21 at 18:35
  • @ekhumoro That worked like a charm, thank you so much for sharing this! Wish I could accept it as an answer.... Thank you both of you for giving me new perspective in the PyQT regarding QTableWidgets, it will be helpful in so many ways in future! ^_^ – Sakib Khan Dec 04 '21 at 18:36

0 Answers0