PDA

View Full Version : benefits SortFilterProxyModel vs own sort in custom table model?



DoTheEvo
3rd November 2016, 20:38
Got a custom QAbstractTableModel, and in it I implemented my own sort method based on column and order.

its fine.

But then I realized that the data are sorted only when user clicks column headers, not when table gets repopulated by new data...
So I am fixing it now along with remembering sorting behaviour between restarts. So that if user clicked sorted by date its sorted by date next time application starts.

But i also came over SortFilterProxyModel that I could implement. Is that the better way?

The speed is my most important aspect, as my program is just a search in a database showing results in tableview as you type queries, and it needs to be instant.

So not sure if one way or the other is more qualified.

d_stranz
3rd November 2016, 21:15
QSortFilterProxyModel usually sits between your model and the view where the model data is displayed. Its purpose is to allow your app to display a sorted version of the model or to extract subsets of it without actually changing the underlying model. This is useful if, for example, you have two different views of the same model. When the model is updated, all views will be automatically updated.

Your implementation confuses the model with the view of the model. Sorting at the view level (by a column click) is a local sort that doesn't change the model. You have propagated that back into the model.

You can either implement a QSortFilterProxyModel, or have your view (or the widget that holds the view if you don't want to derive a new class from QTableView) connect a slot to one of the QAbstractItemModel signals that indicates a change (like modelReset()). In this slot, you can simply call QTableView::sortByColumn() to re-sort the new content. You are probably already connecting to the QHeaderView::sortIndicatorChanged() signal; just remember the arguments for future use when the model changes.

anda_skoa
4th November 2016, 11:14
But i also came over SortFilterProxyModel that I could implement. Is that the better way?

That is a generic way, not necessarily better in ever aspect.

It is "better" in the sense that you don't need to implement anything, i.e. avoiding a source of error/mistake.

Your approach of providing a sorted view on the data is better as it can take the data, its inherent semantic and update behavior into account.



The speed is my most important aspect, as my program is just a search in a database showing results in tableview as you type queries, and it needs to be instant.

I would go with directly presenting the data in the way it should be displayed, i.e. have the model provide the data in the way it should be sorted.
Either by sorting in the model or letting the database already retrieve the values that way.

Cheers,
_

d_stranz
4th November 2016, 17:26
I would go with directly presenting the data in the way it should be displayed, i.e. have the model provide the data in the way it should be sorted.
If speed is the most important aspect, I would agree with this. QSortFilterProxyModel or letting the view sort the data are extra layers that add delays.