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 and QTreeView
Qt 6.11 is out! See what's new in the release blog

QSortFilterProxyModel and QTreeView

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 6 Posters 4.0k Views
  • 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.
  • gde23G Offline
    gde23G Offline
    gde23
    wrote on last edited by
    #2

    The filter is used to filter items. Since in a tree the items are in a tree-like datastructure it will therefore filter an item (and its children), not a index at row/column.
    So, how exactly do you expect the filter to work on a row/column basis in a tree?

    For the mapping of the index you need to convert the index from one to the other model by using mapFromSource() or mapToSource() depending on the direction you are mapping.

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      Qt.Jo.Ha
      wrote on last edited by
      #3

      a7827c6b-6087-4ed5-b07c-1b18c462ddd0-image.png

      i want After the filter is applied, it looks like the following picture.
      3354c578-6dcd-433c-81cf-60ca22b9af22-image.png
      (Situation where filters satisfying item2 and item3 are applied.)

      But now it looks like the following picture

      ea2a740b-3e74-4af4-b244-c03c7d982081-image.png

      This is because item1 does not satisfy the row filter, so all 1 rows have disappeared.

      I want to make the structure of dataModel (ViewModel)-proxy Model (filter)-proxyModel (Mapping = Mapping is 4row -> 1row)

      1 Reply Last reply
      0
      • gde23G Offline
        gde23G Offline
        gde23
        wrote on last edited by
        #4

        Parent is the parent of Item1, Item2 and Item3 right? Then Item2 and Item3 should not be affected by filtering item1. Only the children are.
        And another question: What exactly does the QIdentityProxyModel do??

        Q 1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #5

          You currently have source model -> your proxy model -> QSortFilterProxyModel -> view.
          Any chance you can instead do source model -> QSortFilterProxyModel -> your proxy model -> view ? i.e. filter before applying the transformation.

          The most desired structure is data - QSortFilterProxyModel - QIdentityProxyModel but Crash occurs due to incorrect mapping in QSortFilterProxyModel

          In 99.9999999% of the cases this is due to the implementation of your custom proxy not respecting the strong QAbstractItemModel contract.

          I made a model that inherited the QIdentityProxyModel

          Interesting. I'm actually the original aouthor of QTransposeProxyModel (that basically does the same thing but to the entire model, not only to the children) and I'm pretty sure (and the maintainer of the model-view infrastructure agrees with me) that transformation cannot be done unless:

          • you modify the Qt internals to add your custom proxy as a friend of QAbstractItemModel
          • you pass an invalid index to QIdentityProxyModel::mapToSource

          See this discussion aiming to provide a solution to the problem in Qt6

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          Q 1 Reply Last reply
          1
          • gde23G gde23

            Parent is the parent of Item1, Item2 and Item3 right? Then Item2 and Item3 should not be affected by filtering item1. Only the children are.
            And another question: What exactly does the QIdentityProxyModel do??

            Q Offline
            Q Offline
            Qt.Jo.Ha
            wrote on last edited by Qt.Jo.Ha
            #6

            @gde23 said in QSortFilterProxyModel and QTreeView:

            Parent is the parent of Item1, Item2 and Item3 right? Then Item2 and Item3 should not be affected by filtering item1. Only the children are.
            And another question: What exactly does the QIdentityProxyModel do??

            Thanks for the answer @gde23

            QIdentityProxyModel converts 4 rows to 1 row ! (Listeningly, this is also a kind of mapping ...)

            The reason to use it like this is that the data-QSortFilterProxy-QIdentityProxy structure causes a crash as mentioned above.

            QSortFilterProxy-I'm not sure if the QIdentityProxy structure is bad or I'm wrong.

            What I want most is the data-SortFilterProxy-QIdentityProxy structure.

            1 Reply Last reply
            0
            • VRoninV VRonin

              You currently have source model -> your proxy model -> QSortFilterProxyModel -> view.
              Any chance you can instead do source model -> QSortFilterProxyModel -> your proxy model -> view ? i.e. filter before applying the transformation.

              The most desired structure is data - QSortFilterProxyModel - QIdentityProxyModel but Crash occurs due to incorrect mapping in QSortFilterProxyModel

              In 99.9999999% of the cases this is due to the implementation of your custom proxy not respecting the strong QAbstractItemModel contract.

              I made a model that inherited the QIdentityProxyModel

              Interesting. I'm actually the original aouthor of QTransposeProxyModel (that basically does the same thing but to the entire model, not only to the children) and I'm pretty sure (and the maintainer of the model-view infrastructure agrees with me) that transformation cannot be done unless:

              • you modify the Qt internals to add your custom proxy as a friend of QAbstractItemModel
              • you pass an invalid index to QIdentityProxyModel::mapToSource

              See this discussion aiming to provide a solution to the problem in Qt6

              Q Offline
              Q Offline
              Qt.Jo.Ha
              wrote on last edited by Qt.Jo.Ha
              #7

              Thanks for the answer @VRonin

              Any chance you can instead do source model -> QSortFilterProxyModel -> your proxy model -> view ? i.e. filter before applying the transformation.

              I tried it and Crash came up. I think the reason for crash is because I used InternalPointer as the index passed from QSortFilterProxyModel to QIdentityProxyModel.

              Have you ever seen the article that it is not possible to override Mapping in QSortFilterProxyModel?

              If not, what should I do?

              In 99.9999999% of the cases this is due to the implementation of your custom proxy not respecting the strong QAbstractItemModel contract.

              As I read through the answers, am I correct that I am implementing ProxyModel incorrectly?

              Because will it need to be fixed?

              VRoninV 1 Reply Last reply
              0
              • gde23G Offline
                gde23G Offline
                gde23
                wrote on last edited by
                #8

                Ok, now I understand what you are doing.
                To find out what causes the crash in the approach VRonin suggested, could you try to only apply the filter in a first run to see if it works when used without proxy, and then afterwards add the proxy. Or even better then test the proxy without filter and finally combine them.

                1 Reply Last reply
                0
                • Q Qt.Jo.Ha

                  Thanks for the answer @VRonin

                  Any chance you can instead do source model -> QSortFilterProxyModel -> your proxy model -> view ? i.e. filter before applying the transformation.

                  I tried it and Crash came up. I think the reason for crash is because I used InternalPointer as the index passed from QSortFilterProxyModel to QIdentityProxyModel.

                  Have you ever seen the article that it is not possible to override Mapping in QSortFilterProxyModel?

                  If not, what should I do?

                  In 99.9999999% of the cases this is due to the implementation of your custom proxy not respecting the strong QAbstractItemModel contract.

                  As I read through the answers, am I correct that I am implementing ProxyModel incorrectly?

                  Because will it need to be fixed?

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #9

                  @Qt-Jo-Ha said in QSortFilterProxyModel and QTreeView:

                  am I correct that I am implementing ProxyModel incorrectly?

                  Yes, and I'm going even further in saying that it's impossible to create such a proxy correctly in Qt5 without manually building a mirror of the entire tree structure in your proxy or using one of the 2 "hacks" mentioned above due to QTBUG-83911

                  Generally speaking, however, the easier way to find errors in the model implementation is by using the model test

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  1
                  • B Offline
                    B Offline
                    Bonnie
                    wrote on last edited by
                    #10

                    Actually I think QIdentityProxyModel is not designed to "converts 4 rows to 1 row".
                    So is that possible to move that mapping to a subclassed QSortFilterProxyModel, then there can be only one proxy...

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      macfanpl
                      wrote on last edited by
                      #11

                      Qt is using logic thats turned inside-out

                      With normal programming language, TreeNode inherits its properties from master node , but not in the qt. Here master node inherits from child....

                      FUCKED UP

                      jsulmJ 1 Reply Last reply
                      -5
                      • M macfanpl

                        Qt is using logic thats turned inside-out

                        With normal programming language, TreeNode inherits its properties from master node , but not in the qt. Here master node inherits from child....

                        FUCKED UP

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @macfanpl Please read and respect https://forum.qt.io/topic/113070/qt-code-of-conduct

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        4
                        • Q Offline
                          Q Offline
                          Qt.Jo.Ha
                          wrote on last edited by
                          #13

                          Thanks for the answer, everyone finally solved this problem.

                          The reason was that the wrong index was created in the index function that was overridden in the last QIdentityProxyModel.

                          I tried to keep the parent item and the child item separately, so using internalPointer seems to have contributed to the crash.

                          1 Reply Last reply
                          2

                          • Login

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