Search code examples
c++qtmodel-view

Removing rows from QAbstractTableModel derived class don't work, why?


I have a Qt application for which I derived my own model class from QAbstractTableModel. I have implemented the necessary methods as prescribed in the documentation. When I call removeRows method the changes are correct in my View (the rows I wanted to remove are removed).

But somehow, the operations on the model doesn't seem to be propagated to the QList I use in the model to store my data. When I save the values stored in the QList to the disk, it look like nothing was erased from it by removeRows.

Here is what my removeRows implementation looks like (it is based on the code from the book Advanced Qt Programming, Chapter 3, p.125):

bool MyModel::removeRows(int row, int count, const QModelIndex&)
{
    beginRemoveRows( QModelIndex(), row, row + count - 1);

    for (int i = 0; i < count; ++i) {
        mMyQList.removeAt(row);
    }

    endRemoveRows();

    return true;
}

How do I fix this? What did I miss?

Thanks!


Solution

  • It turns out that nothing was wrong with my implementation of removeRows.

    The save method was called by my unit tests just before showing my dialog. The dialog was not calling the save method at all.

    No wonder the change were visible in the View and not in the output file...