Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Simple TreeView based on a ListModel & a separator

Simple TreeView based on a ListModel & a separator

Scheduled Pinned Locked Moved Solved QML and Qt Quick
12 Posts 4 Posters 738 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.
  • S Offline
    S Offline
    swurl
    wrote on last edited by
    #1

    I already have a ListView and a ListModel properly set up. However, the data in the model needs to be separated into a tree, as the data itself is separated by slashes (e.g. /RootItem/ChildItem). Is there a simple way to "convert" the ListModel data into a TreeView with proper separation based on the slashes?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      swurl
      wrote on last edited by
      #8

      Switching to a QStandardItemModel seemed to work just fine, it makes handling of parent/child easy.

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

        If the model is built in C++ (or Python) then you can use a QAbstarctProxyModel subclass to implement it, if it's built in QML then I'm afraid you need to rebuild a separate model manually

        "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
        • S Offline
          S Offline
          swurl
          wrote on last edited by
          #3

          It's built in C++. Moreso what I'm asking is how I would be able to split things up into a tree based on an existing C++ model. The ProxyModel documentation is pretty sorely lacking.

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

            That’s an abstract class, the docs can’t tell you how to implement custom stuff. See https://doc.qt.io/qt-6/model-view-programming.html for a more in-depth explanation

            "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
              Bob64
              wrote on last edited by
              #5

              I think a QAbstractProxyModel needs a QAbstractItemModel as a source. In other words, it is not a means to adapt something like a QAbstractListModel to a QAbstractItemModel.

              If @swurl is starting with a list model, I think they will need to write their own adaptor that implements QAbstractItemModel as this is the model type needed for a tree view.

              VRoninV JonBJ 2 Replies Last reply
              0
              • B Bob64

                I think a QAbstractProxyModel needs a QAbstractItemModel as a source. In other words, it is not a means to adapt something like a QAbstractListModel to a QAbstractItemModel.

                If @swurl is starting with a list model, I think they will need to write their own adaptor that implements QAbstractItemModel as this is the model type needed for a tree view.

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

                @Bob64 said in Simple TreeView based on a ListModel & a separator:

                it is not a means to adapt something like a QAbstractListModel to a QAbstractItemModel.

                that's exactly what it's meant to do

                "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 Bob64

                  I think a QAbstractProxyModel needs a QAbstractItemModel as a source. In other words, it is not a means to adapt something like a QAbstractListModel to a QAbstractItemModel.

                  If @swurl is starting with a list model, I think they will need to write their own adaptor that implements QAbstractItemModel as this is the model type needed for a tree view.

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

                  @Bob64 said in Simple TreeView based on a ListModel & a separator:

                  it is not a means to adapt something like a QAbstractListModel to a QAbstractItemModel.

                  As @VRonin has written. Are you aware that QAbstractListModel derives from QAbstractItemModel so it does need "adapting", it already is one?

                  B 1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    swurl
                    wrote on last edited by
                    #8

                    Switching to a QStandardItemModel seemed to work just fine, it makes handling of parent/child easy.

                    1 Reply Last reply
                    0
                    • S swurl has marked this topic as solved on
                    • JonBJ JonB

                      @Bob64 said in Simple TreeView based on a ListModel & a separator:

                      it is not a means to adapt something like a QAbstractListModel to a QAbstractItemModel.

                      As @VRonin has written. Are you aware that QAbstractListModel derives from QAbstractItemModel so it does need "adapting", it already is one?

                      B Offline
                      B Offline
                      Bob64
                      wrote on last edited by
                      #9

                      @JonB said in Simple TreeView based on a ListModel & a separator:

                      As @VRonin has written. Are you aware that QAbstractListModel derives from QAbstractItemModel so it does need "adapting", it already is one?

                      Yes I am aware - or at least I should be - so I am not sure what I was thinking there!

                      It is possible that my thinking was influenced by another thought I was having, that a list model like this does not necessarily directly map to a tree. I am recalling similar things I have worked with where the list contains only paths to leaf nodes. It seems like mapFromSource/mapToSource kind of assumes a one-to-one mapping, in which case I think that sort of list model would need to be expanded to an intermediate list model that adds in interior nodes as rows.

                      JonBJ 1 Reply Last reply
                      0
                      • B Bob64

                        @JonB said in Simple TreeView based on a ListModel & a separator:

                        As @VRonin has written. Are you aware that QAbstractListModel derives from QAbstractItemModel so it does need "adapting", it already is one?

                        Yes I am aware - or at least I should be - so I am not sure what I was thinking there!

                        It is possible that my thinking was influenced by another thought I was having, that a list model like this does not necessarily directly map to a tree. I am recalling similar things I have worked with where the list contains only paths to leaf nodes. It seems like mapFromSource/mapToSource kind of assumes a one-to-one mapping, in which case I think that sort of list model would need to be expanded to an intermediate list model that adds in interior nodes as rows.

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by
                        #10

                        @Bob64
                        I'm sorry I don't know how this relates to your list model. But in the case of a tree model, for a tree view display, a Qt tree model will need to have nodes as well as the leaves.

                        B 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @Bob64
                          I'm sorry I don't know how this relates to your list model. But in the case of a tree model, for a tree view display, a Qt tree model will need to have nodes as well as the leaves.

                          B Offline
                          B Offline
                          Bob64
                          wrote on last edited by
                          #11

                          @JonB sorry I wasn't clear. I was thinking of something that regularly cropped up in some past projects where we had to transform lists of path-like data into tree models. Consider a list of paths that are all of this form:

                          /path/to/leaf

                          Even though the list only contains entries to leaves, there is certainly sufficient information in the data to infer a tree structure, but I don't think the proxy would allow one to map a list model of this form directly to a tree model.

                          Anyway, this was what was in my mind and what was colouring my response to the question. Apologies if I caused any confusion.

                          JonBJ 1 Reply Last reply
                          0
                          • B Bob64

                            @JonB sorry I wasn't clear. I was thinking of something that regularly cropped up in some past projects where we had to transform lists of path-like data into tree models. Consider a list of paths that are all of this form:

                            /path/to/leaf

                            Even though the list only contains entries to leaves, there is certainly sufficient information in the data to infer a tree structure, but I don't think the proxy would allow one to map a list model of this form directly to a tree model.

                            Anyway, this was what was in my mind and what was colouring my response to the question. Apologies if I caused any confusion.

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by
                            #12

                            @Bob64
                            If you have leaf /path/to/leaf to be split on /s at some level you will have to create/return/treat as existing nodes /, /path and /path/to as well as leaf /path/to/leaf in order to keep Qt tree model/view code happy.

                            You have two basic approaches:

                            • Actually split all your strings into necessary nodes & leaves into their own model (e.g. a QStandardItemModel can be thus populated and is ready for you to use). This obviously means a separate representation/storage for the tree model compared to wherever you store the strings/list model.

                            • You could do it with a proxy model which has the strings as the underlying data but you would have to parse them each time, do the splitting temporarily/dynamically in memory and return the appropriate nodes, leaves and counts of children as Qt queries the model. That would mean only the strings were permanently maintained.

                            The second case might be wanted if the strings change frequently or you want changes in the tree model to reflect directly to underlying strings. But the second case is probably easier to write and works fine if the tree model/strings not not change.

                            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