QSortFilterProxyModel will filter but not sort
-
wrote on 12 Aug 2021, 14:19 last edited by ocgltd 8 Dec 2021, 14:53
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)
-
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)
wrote on 12 Aug 2021, 14:59 last edited by@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:
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)
wrote on 12 Aug 2021, 15:22 last edited by@KroMignon 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)
-
@KroMignon 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)
wrote on 12 Aug 2021, 21:14 last edited by@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.
-
@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.
wrote on 13 Aug 2021, 06:10 last edited by@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. -
@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.wrote on 13 Aug 2021, 18:23 last edited by@JonB My experience is very different than yours. Changing filters (invalidFilter) does not trigger a sort - in fact it never sorts unless I manually trigger at least one sort. So I call sort in the proxy constructor and now it works.
-
@JonB My experience is very different than yours. Changing filters (invalidFilter) does not trigger a sort - in fact it never sorts unless I manually trigger at least one sort. So I call sort in the proxy constructor and now it works.
wrote on 13 Aug 2021, 19:16 last edited by@ocgltd , @KroMignon
Upon review, that sample code does goproxyView->sortByColumn(1, Qt::AscendingOrder);
. I searched it, and my code, forsort(
. So maybe that is in my code and would explain.
1/7