undelete row from QTableView

104 views Asked by At

I am new to PyQt5, I am working with a QTableView. After successfuly knew how te delete selected rows.. I have googled a lot to know ho to undelete a selected deleted row from the table (& by sequence from the TableModel) with no result...

So far, I could know if the row is marked deleted based on the header data:

if self.items_Model.itemsModel.headerData(row.row(), QtCore.Qt.Vertical) == "!":
    # self.items_Model.itemsModel.revertRow(row.row())
    self.items_Model.itemsModel.selectRow(row.row())

I tried revertRow(rowIndex) and selectRow() but these methods repopulate the row from the database and does not remove the flag "!"; meaning - as I supposed - that on Submit, it will be deleted.

Please, any help.

1

There are 1 answers

0
Zeinab Yasine On BEST ANSWER

It's weird that nobody here had answered my question...

After several tries... Including closing the editor and relaunching it...

It appears that to return the row to its previous state - in this case "not marked deleted"- we must use the "revert" method. But the mark "!" does not change unless we remove focus of the row to be undeleted i.e. select another row.

this is the whole code of my delete method:

def _deleteSelectedRows(self):
    select = self.mainForm.tblItems.selectionModel()

    if select.hasSelection():
        selectedRows = select.selectedRows()

        for r in selectedRows:
            rowFlag = self.items_Model.itemsModel.headerData(r.row(), QtCore.Qt.Vertical)
            if rowFlag == "!":
                self.items_Model.itemsModel.revertRow(r.row())
            else:
                self.items_Model._deleteRowByIndex(r.row())