[Interest] Can QSortFilterProxyModel sort the filtered data only?

André Somers andre at familiesomers.nl
Wed Apr 9 09:52:06 CEST 2014


Mark Gaiser schreef op 8-4-2014 17:41:
> Hi,
>
> When reading through the QSortFilterProxyModel i get the feeling that
> sorting is done on the source model regardless of a filter.
>
> That is no problem with small models since sorting is quite fast, but
> that can be a bit of a pain if you have millions of entries (which
> filters reduce to hundreds) then sorting still seems to sort all
> entries (as if nothing is filtered) which takes a bit of time.
If you have millions of entries to filter, I doubt QSFPM is your best 
choice to begin with. In such cases, you probably have your data in a 
database anyway. The database is able to sort and filter your data way 
more efficiently than QSFPM will ever be able to do. It's whole 
structure is designed around doing tasks like that efficiently, using 
things like indices. So, instead of filtering millions of records in 
QSFPM, I'd use the capabilities of your data backend instead.
>
> A detour that would probably work is wrapping a QSortFilterProxyModel
> (with just the filter) in another QSortFilterProxyModel and sorting on
> that (since it's source would be a filtered model). This approach
> basically makes one a "QFilterProxyModel" and the other a
> "QSortProxyModel" which smells wrong.
>
> Another way would be to re-implement QAbstractProxyModel and
> implementing my own sorting and filtering, but i'd like to prevent
> that if possible.
>
> What i'm hoping for is some option in QSortFilterProxyModel where you
> can define how you want to sort:
> - Sort source model (sorts all data)
> - Sort proxy model (sorts data that passed the filtered)
It's not that simple. At first glance, you'd think that first filtering 
and then sorting would be optimal. But that really depends on how you 
implement the filtering and sorting. If you do naive filtering (which, 
AFAIK, QSFPM does) where you simply test each item one by one to see if 
it matches the filters, then it would be most efficient to first filter 
and then sort*. But if you have more knowledge on the dataset, perhaps 
because you know it is already sorted, or you have an index of some 
sort, you may be able to do your filtering way more efficiently, 
depending on the exact filter type of course. A filter like "all entries 
starting with string <ABC>" for instance would be very fast if you can 
use the knowledge that the dataset is sorted on that field. I am not 
sure how QSFPM is implemented, but if it first sorts and then filters, 
then that would be silly indeed if it uses the naïve filtering I think 
it does. Even in this case, such an option would not be needed I think. 
If you cannot do advanced filtering or sorting based on indices or other 
knowledge of the underlying data, then there is no way to optimize other 
than minimizing thouching the data. Configuration is not needed for that.


André

*) though perhaps you could come up with a scheme that does both at the 
same time somehow, doing the filtering the first time an item is 
'touched' in the sorting algorithm pershaps. Perhaps QSFPM is that clever?

>
> But such an option doesn't seem to exist.
>
> Cheers,
> Mark
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest




More information about the Interest mailing list