Inconvenience when sorting a QTableView with QSortFilterProxyModel
-
I am using QSortFilterProxyModel to sort my QTableView which has a model that inherits from QAbstractTableModel. Visually the TableView is sorted properly but the entries of the model does not get sorted. As a result, whenever a specific row data changes, the wrong entry gets updated.
Here is the code that I am using:ui->tableView->setSortingEnabled(true); QSortFilterProxyModel *proxyModel2 = new QSortFilterProxyModel(ui->tableView); proxyModel2->setSourceModel(&containertei); ui->tableView->setModel(proxyModel2); ui->tableView->sortByColumn(2, Qt::AscendingOrder);
containertei is the model that inherits from QAbstractTableModel and it gets created before setting the QSortFilterProxyModel
-
I am using QSortFilterProxyModel to sort my QTableView which has a model that inherits from QAbstractTableModel. Visually the TableView is sorted properly but the entries of the model does not get sorted. As a result, whenever a specific row data changes, the wrong entry gets updated.
Here is the code that I am using:ui->tableView->setSortingEnabled(true); QSortFilterProxyModel *proxyModel2 = new QSortFilterProxyModel(ui->tableView); proxyModel2->setSourceModel(&containertei); ui->tableView->setModel(proxyModel2); ui->tableView->sortByColumn(2, Qt::AscendingOrder);
containertei is the model that inherits from QAbstractTableModel and it gets created before setting the QSortFilterProxyModel
@KlodKrichen said in Inconvenience when sorting a QTableView with QSortFilterProxyModel:
but the entries of the model does not get sorted.
Which model? It will not sort the source model (
containertei
), it will sort the proxy model (proxyModel2
).As a result, whenever a specific row data changes, the wrong entry gets updated.
Meaning what exact code? Be careful in your code whether you want to refer to/get
QModelIndex
es from the source model or the proxy model.... -
I want to sort the containertei model at the same time the view gets sorted.
I get exactly what you mean, so how do I explicitly get the QModelIndex from the source model rather then the proxy model? -
When you want to change a specific row in the view then either go through proxy's setData that will internally translate to the right base model index and call its setData, or, manually translate the proxy index to the base model index with mapToSource and operate on that.
-
I want to sort the containertei model at the same time the view gets sorted.
I get exactly what you mean, so how do I explicitly get the QModelIndex from the source model rather then the proxy model?@KlodKrichen said in Inconvenience when sorting a QTableView with QSortFilterProxyModel:
I want to sort the containertei model at the same time the view gets sorted.
Why? In that case, why are you interposing a
QSortFilterProxyModel
at all? It's one or the other:-
If you have a
QSortFilterProxyModel
, that does the sorting, the underlying model does not get sorted, else it's pointless. -
If you want to directly sort the underlying model (for some unknown reason, do you really want to?) then do not interpose a
QSortFilterProxyModel
.
Otherwise if you do have the proxy then read carefully what @Chris-Kawa has said.
-
-
Thanks for the suggestion seems clear enough. The reason why I want to keep the same order in the view and the source model is that I have a state for each table row entry, depending on that state (reviewed / in revision) I am permitting/or not the user to edit the specific row. So whenever the table gets sorted, the state of each row is not the appropriate one since I am calling the Data method of the source model. So maybe the solution is to go through proxy's setData and Data method as @Chris-Kawa suggested
-
Thanks for the suggestion seems clear enough. The reason why I want to keep the same order in the view and the source model is that I have a state for each table row entry, depending on that state (reviewed / in revision) I am permitting/or not the user to edit the specific row. So whenever the table gets sorted, the state of each row is not the appropriate one since I am calling the Data method of the source model. So maybe the solution is to go through proxy's setData and Data method as @Chris-Kawa suggested
@KlodKrichen This sounds like that state should be a custom user role in the base model and accessed through the data/setData methods, so indices get translated automatically when using a proxy model on top of it. Sorting the base model is unnecessary here. Whether a user can edit given row would then be managed simply by implementing the flags method of the model and checking what the state is for that index.
-
Thank you very much, thanks to your suggestion I managed to solve the issue without sorting the source model.