Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QSortFilterProxyModel and QTreeView



  • hi, i have a treeview

    The tree view looks like this
    499dd535-17ab-4884-857a-6d79b3879494-image.png

    and I made a model that inherited the QIdentityProxyModel

    After applying the model, it looks like this

    6e2dd571-9421-4670-9d01-72f8cbe40dd5-image.png

    If I finally apply QSortFIlterProxyModel here, the filter doesn't work as I want , So I ask.

    1. Is it possible to filter from QSortFilterProxyModel to index (Row and column filters)

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

    plz answer me ..!



  • 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.



  • 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)



  • 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??



  • 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



  • @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.



  • 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?



  • 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.



  • @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



  • 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...



  • 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


  • Qt Champions 2019



  • 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.


Log in to reply