Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSortFilterProxyModel what types can you actually sort by?

QSortFilterProxyModel what types can you actually sort by?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 902 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #1

    I have wasted 2 hours scratching my head over why sorting the rows I have through a QSortFilterProxyModel on my model seems to do no sorting when I call QSortFilterProxyModel::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 phrase

    QSortFilterProxyModel provides a generic sort() reimplementation [...] and that understands several data types, including int, QString, and QDateTime.

    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 QDate directly; 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 types QSortFilterProxyModel::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 of QVariant types 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...?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      JonBJ 1 Reply Last reply
      1
      • SGaistS SGaist

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @SGaist
        The whole point is, I do not have a custom class! I have been trying to discover just what types are dealt with by the in-built QSortFilterProxyModel::sort() method "natively", without me having any custom class or my own lessThan().

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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_N2Qt15CaseSensitivityEb

          And as expected it can compare it's internal integer/real/date types and all other is falling back to string comparison

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          JonBJ 1 Reply Last reply
          3
          • Christian EhrlicherC Christian Ehrlicher

            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_N2Qt15CaseSensitivityEb

            And as expected it can compare it's internal integer/real/date types and all other is falling back to string comparison

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @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 any QVariants 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. Python datetime.date type <-> C++/QVariant::QDate type. Somehow, however, it just does not seem to be doing that in the case of QSortFilterProxyModel::sort(). It may turn out that this is a PyQt issue....

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              I meant the operator < for your custom types.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              JonBJ 1 Reply Last reply
              0
              • SGaistS SGaist

                I meant the operator < for your custom types.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @SGaist
                I am saying that I do not have any custom types! That's the whole point of why I was asking which types QSortFilterProxyModel::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.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved