Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    3
    3
    779
    Loading More Posts
    • 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.
    • Thuan_Firelight
      Thuan_Firelight last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        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.

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

        1 Reply Last reply Reply Quote 0
        • VRonin
          VRonin last edited by

          I think you are describing exactly the functionality of KDescendantsProxyModel

          "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 Reply Quote 1
          • First post
            Last post