QSortFilterProxyModel will filter but not sort
-
I have successfully implemented QSortFilterProxyModel and the data is being filtered in my view as soon as my filter criteria change (and I call invalidate). I am now trying to implement the sort functionality, but it's not working. The lessThan function is never being called (I added a qDebug statement to confirm).
My method is declared like this:
bool lessThan(const QModelIndex &left,const QModelIndex &right) const override;
Is there some property I must set or method I must call to tell the proxy to do sorting? Do I have to call sort at some point? (Though the column parameters makes no sense given I'm doing a custom sort)
-
@ocgltd said in QSortFilterProxyModel will filter but not sort:
Is there some property I must set or method I must call to tell the proxy to do sorting? Do I have to call sort at some point? (Though the column parameters makes no sense given I'm doing a custom sort)
Yes, at least you have to start a first sorting by calling
sort(0, Qt::DescendingOrder)
orsort(0, Qt::AscendingOrder)
-
@ocgltd said in QSortFilterProxyModel will filter but not sort:
When should I call that? In the QSortFilterProxyModel constructor?
Do I also need to call it every time the filter changes? (Or on some signal)I do it after setting up source model.
Per default, dynamicSortFilter() is true, so on each model change sort() will be triggered automatically, but the first sort have to be triggered manually.This is my experience with QSortFilterProxyModel.
-
@KroMignon said in QSortFilterProxyModel will filter but not sort:
Yes, at least you have to start a first sorting by calling sort(0, Qt::DescendingOrder) or sort(0, Qt::AscendingOrder)
but the first sort have to be triggered manually.
I use
QSortFilterProxyModel
and have never called anysort()
, I don't know why you (or @ocgltd?) say you have to? The proxy will sort automatically when first attached. There is a Basic Sort/Filter Model Example which illustrates the minimum needed, and it does notsort()
.@ocgltd
Whenever you do something like callsetFilter...()
to change the filter theQSortFilterProxyModel
will invalidate and re-sort itself automatically.