⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

TreeView with functionality to go back and forth flat list and hierarchical view?

  • Hi all,

    I am looking for the best approach or recommended approach to implementing a tree view which can display as a flat list as well as hierarchical view via a toggle button.

    The target widget taking the model is a QTreeView. I already have a tree model class that implements QAbstractItemModel and the hierarchical aspect is all sorted. What I need now is a way for the same tree view to display the items as a flat list. I can either create a brand new model and pass that to the tree view or write a class implementing a QAbstractProxyModel and pass that to the tree view instead. I am attempting option 2. I was looking at deriving QIdentityProxyModel with a flag in my derived class that determines if I should do some other logic if displaying as a flat list or forward calls (e.g. mapFromSource, mapToSource, etc) to the QIdentityProxyModel if displaying as a hierarchy.

    Does this sound like a sensible approach? I had a go at this and it seems Qt is still passing me old proxy indices created by QIdentityProxyModel even when the view is in the flat list model. Is there something I need to do to force the UI to refresh so it would call index() to generate the new proxy index for each item?

  • Lifetime Qt Champion


    What about having two views on your model rather that trying to fit everything in a tree view ?

    You can then use a QStackedLayout and switch between the views.

  • I think you are describing exactly the functionality of KDescendantsProxyModel

Log in to reply