Search code examples
pythonpython-3.xpyqtpyqt5qtablewidget

Qtablewidget Deleting row issue


Need some expert advice here.

Am using PyQT5 and Python 3 to set up an UI. I have created a table with Qtablwiwdget and I was able to get most of the controls. However, am stuck in a delete operation. I have created a Delete button which deletes the selected row. Am able to achieve this by passing currentrow value to the removerow function.

Now the issue is that, after every delete, the currentrow count gets decreased by 1 and hence if I click the delete button multiple times, the rows above the selected row is being deleted rather than the ones below.

Here is my code :

        self.DataTable = QTableWidget(self)
        self.DataTable.setColumnCount(3)
        self.DataTable.setRowCount(1)
        self.DataTable.setHorizontalHeaderLabels(["Name","Age","Location"])
        self.DataTable.horizontalHeaderItem(0).setTextAlignment(Qt.AlignHCenter)
        self.DataTable.horizontalHeaderItem(1).setTextAlignment(Qt.AlignHCenter)
        self.DataTable.horizontalHeaderItem(2).setTextAlignment(Qt.AlignHCenter)
        self.DataTable.resize(450, 400)

        self.Delete_butn.clicked.connect(self.On_Delete_Click) # button click

#slot

@pyqtSlot()
    def On_Delete_Click(self):
        SelectedRow = self.DataTable.currentRow()
        rowcount = self.DataTable.rowCount()

        if rowcount==0:  # gives a pop up when now more rows are there to delete
            msg = QMessageBox(self)
            msg.setIcon(QMessageBox.Warning)
            msg.setText("NOTE:")
            msg.setInformativeText("\n No more rows to delete! \t\t")
            msg.setWindowTitle("WARNING!")
            # msg.setDetailedText("The details are as follows:")
            msg.setStandardButtons(QMessageBox.Ok)
            msg.buttonClicked.connect(self.msgbtn)
            retval = msg.exec_()



        elif SelectedRow==-1: # Gives pop up when no rows are selected

            msg = QMessageBox(self)
            msg.setIcon(QMessageBox.Warning)
            msg.setText("NOTE:")
            msg.setInformativeText("\n Please select the row to be deleted! \t\t")
            msg.setWindowTitle("WARNING!")
            # msg.setDetailedText("The details are as follows:")
            msg.setStandardButtons(QMessageBox.Ok)
            msg.buttonClicked.connect(self.msgbtn)
            retval = msg.exec_()


        else:
            self.DataTable.removeRow(SelectedRow)

What i want to achieve is that, if am having 8 rows (from 0-7) having correct values and at rows 3-6 having wrong values, I'll be clicking row 3 first, click delete . On deleting , the row shifts up and I should be deleting row 3 again . But its not happening. After 1st delete operation, current row is decreasing from 3 to 2 as a result am losing values on that was present on top of the wrong valued cells


Solution

  • The problem is that the row associated with the currentIndex() decreases by 1 after deleting the row that is the same as currentRow(), so the solution is to establish that the currentIndex has the same location after removing the row:

    # ...
    else:        
        self.DataTable.removeRow(SelectedRow)
        ix = self.DataTable.model().index(
            SelectedRow, self.DataTable.currentColumn()
        )
        self.DataTable.setCurrentIndex(ix)
    # ...