Reputation: 21
I am using QStandardItemModel as source model to display data in one table view. one more table view to display filter data once apply the filter operation on QStandardItemmodel using Custom filter proxy model(subclass of QSortFilterProxy model).
When I am trying to remove the data from that custom filter proxy model (Sub class of QSortFilterProxy model) it will also remove that data from its source model (QStandardItemModel) . How to avoid that?
Sample Code:
MySortFilterProxyModel *proxyModel;
QStandardItemModel *model
QTreeView *sourceView;
QTreeView *proxyView;
proxyModel->setSourceModel(model);
sourceView->setModel(model);
proxyView->setModel(proxyModel);
When I do
proxyModel->removeRows(0,proxyModel->rowCount());
It will remove the data from its source model also ( QStandardItemModel *model
).
Upvotes: 1
Views: 1418
Reputation: 4050
You have to reimplement your own virtual bool removeRows(...). Check Qt doc:
If you implement your own model, you can reimplement this function if you want to support removing. Alternatively, you can provide your own API for altering the data.
QSortFilterProxyModel should be used to sort or filter data.
The QSortFilterProxyModel class provides support for sorting and filtering data passed between another model and a view.
I recommend to use a flag to check the state of an element and filter it, example:
enum State {Valid, Invalid}
Now in your QSortFilterProxyModel::filterAcceptsRow(..) add a new condition to check the state of the element:
const State state = _sourceModel->data(index, Qt::UserRole).toInt(&);
return state == Valid;
In your sourceModel you should return the state of the element in the virtual function data() for Qt::UserRole or any role you want.
When you want to remove a row from the QSortFilterProxyModel just change the state of the element and call invalidate() to update the filter.
If you want to remove it globally from the source model, use removeRows.
Upvotes: 1