How to implement the removeRow () function for the custom QT model?

advertisements

I have searched QT doc for reference but I got some questions. Here is the implementation in QT doc:

bool TableModel::removeRows(int position, int rows, const QModelIndex &index)
{
    Q_UNUSED(index);
    beginRemoveRows(QModelIndex(), position, position + rows - 1);

    for (int row = 0; row < rows; ++row) {
        listOfPairs.removeAt(position);
    }

    endRemoveRows();
    return true;
}

I think it uses index.row in the tableview to directly locate the data in the model. But what if I enable sorting for the tableview? After sorting, the index in the tableview doesn't correspond to the data in the model. The first row in the tableview may be the third data in the model.

How can I locate the data in model through tableview? And are there any better implementations for removeRow() function?


you can use tableWidgetItem->setData(0, Qt::UserRole, "Desired Identity"); it will be always unique even when you sort the rows. refer QTableWidget find a row through userdata