QSortFilterProxyModel what types can you actually sort by?
-
I have wasted 2 hours scratching my head over why sorting the rows I have through a
QSortFilterProxyModelon my model seems to do no sorting when I callQSortFilterProxyModel::sort(0). And that does not tell you whether/why it can't sort.Reading http://doc.qt.io/qt-5/qsortfilterproxymodel.html#sort, and even the base classes' implementations, left me none the wiser.
Then I happenstanced across http://doc.qt.io/qt-5/qsortfilterproxymodel.html#sorting (not even in
sort()'s description) and the little phraseQSortFilterProxyModelprovides a genericsort()reimplementation [...] and that understands several data types, includingint,QString, andQDateTime.That's it. Come on, this is barely "documentation" it's more just "touchy-feely". Where anywhere in the documentation does it actually state which types are or are not sortable, please?
My problem is not helped by the fact that I am Python/PyQt: we don't use Qt types like
QDatedirectly; my type is sortable and Python types are converted to/from Qt types automatically, but that is not the point. I need to know the typesQSortFilterProxyModel::sort()can sort without needing to go down the custom sort sub-classing route.EDIT
Oh, I've just gone into http://doc.qt.io/qt-5/qsortfilterproxymodel.html#lessThan, which didn't occur to me to look at as I specifically do not want to write my own class, where I finally see a list ofQVarianttypes which I guess are what tells me what the types are. To me this is not obvious at all that one should look here, but I guess that is my list...? -
Hi,
From the top of my head and if memory serves well, if you implement the less than operator for your custom class, then you should be good to go.
-
Hi,
From the top of my head and if memory serves well, if you implement the less than operator for your custom class, then you should be good to go.
-
Just take a look at the sources: https://code.woboq.org/qt5/qtbase/src/corelib/itemmodels/qsortfilterproxymodel.cpp.html#ZNK21QSortFilterProxyModel8lessThanERK11QModelIndexS2
and specifically https://code.woboq.org/qt5/qtbase/src/corelib/itemmodels/qabstractitemmodel.cpp.html#_ZN25QAbstractItemModelPrivate17isVariantLessThanERK8QVariantS2_N2Qt15CaseSensitivityEbAnd as expected it can compare it's internal integer/real/date types and all other is falling back to string comparison
-
Just take a look at the sources: https://code.woboq.org/qt5/qtbase/src/corelib/itemmodels/qsortfilterproxymodel.cpp.html#ZNK21QSortFilterProxyModel8lessThanERK11QModelIndexS2
and specifically https://code.woboq.org/qt5/qtbase/src/corelib/itemmodels/qabstractitemmodel.cpp.html#_ZN25QAbstractItemModelPrivate17isVariantLessThanERK8QVariantS2_N2Qt15CaseSensitivityEbAnd as expected it can compare it's internal integer/real/date types and all other is falling back to string comparison
@Christian-Ehrlicher
Thanks, that was useful. Remember it's all very difficult for me as I come from Python/PyQt, so I don't even know/have access to anyQVariants it might be using for me. That all happens "behind the scenes". What happens everywhere else through PyQt is that it is supposed to auto-convert for me, e.g. Pythondatetime.datetype <-> C++/QVariant::QDatetype. Somehow, however, it just does not seem to be doing that in the case ofQSortFilterProxyModel::sort(). It may turn out that this is a PyQt issue.... -
I meant the
operator <for your custom types. -
@SGaist
I am saying that I do not have any custom types! That's the whole point of why I was asking which typesQSortFilterProxyModel::sort()intends to handle "out-of-the-box"!If it makes any difference, I have now raised a question with the PyQt group, as I am finding from Python/PyQt it just refuses to do any sorting at all on many column types, I don't know what's going on.