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. Convert list into a Tree Model by role

Convert list into a Tree Model by role

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 2.2k 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.
  • ProgSysP Offline
    ProgSysP Offline
    ProgSys
    wrote on last edited by
    #1

    Hello,

    I have a custom flat item model which inherits QAbstractItemModel and also has multiple custom roles (columns), like 'species' or 'age'.
    Is there an easy way to convert this 'List Model' into a 'Tree Model', with a given role (column)?

    I know there is QSortFilterProxyModel, but it seems it can only filter and sort my model and not dynamically reconstruct it into a tree.

    Example:
    What I have:

    Name   |Species| Age
    ______________________
    Oliver | Monkey| 8
    David  | Monkey| 10
    Tiger  | Cat   | 4
    Loki   | Cat   | 8
    Bella  | Dog   | 10
    Toby   | Dog   | 8
    

    The goal:

    Sort by species:          Sort by age:
    Monkey                    4
    |_________ Oliver         |_________ Tiger
    |_________ David          8
    Cat                       |_________ Oliver
    |_________ Tiger    OR    |_________ Loki
    |_________ Loki           |_________ Toby
    Dog                       10
    |_________ Bella          |_________ Bella
    |_________ Toby           |_________ David
    

    Is there a Proxy Model that can do something like this or do I need to modify my data to archive this?

    Thanks!

    K 1 Reply Last reply
    0
    • ProgSysP ProgSys

      Hello,

      I have a custom flat item model which inherits QAbstractItemModel and also has multiple custom roles (columns), like 'species' or 'age'.
      Is there an easy way to convert this 'List Model' into a 'Tree Model', with a given role (column)?

      I know there is QSortFilterProxyModel, but it seems it can only filter and sort my model and not dynamically reconstruct it into a tree.

      Example:
      What I have:

      Name   |Species| Age
      ______________________
      Oliver | Monkey| 8
      David  | Monkey| 10
      Tiger  | Cat   | 4
      Loki   | Cat   | 8
      Bella  | Dog   | 10
      Toby   | Dog   | 8
      

      The goal:

      Sort by species:          Sort by age:
      Monkey                    4
      |_________ Oliver         |_________ Tiger
      |_________ David          8
      Cat                       |_________ Oliver
      |_________ Tiger    OR    |_________ Loki
      |_________ Loki           |_________ Toby
      Dog                       10
      |_________ Bella          |_________ Bella
      |_________ Toby           |_________ David
      

      Is there a Proxy Model that can do something like this or do I need to modify my data to archive this?

      Thanks!

      K Offline
      K Offline
      karlheinzreichel
      wrote on last edited by
      #2

      @ProgSys
      see http://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html

      ProgSysP 1 Reply Last reply
      0
      • K karlheinzreichel

        @ProgSys
        see http://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html

        ProgSysP Offline
        ProgSysP Offline
        ProgSys
        wrote on last edited by ProgSys
        #3

        @karlheinzreichel But wouldn't this require that I modify my data? I want to dynamically convert any given flat model into a hierarchical one.

        I found that QAbstractProxyModel has 'mapFromSource' and 'mapToSource', but I am not sure how to modify them, as they would need to point to rows that don't exist in the data.

        Maybe this Image makes it more clear what I need:
        0_1513281149983_list_to_hierarchical.png

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

          This is hard but the opposite (i.e. tree to list) is trivial. any chance you can structure your original model as a tree instead and convert it to a list later on?

          "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
          • K Offline
            K Offline
            karlheinzreichel
            wrote on last edited by
            #5

            @ProgSys I want to dynamically convert any given flat model into a hierarchical one.

            But when you will convert ANY given flat model into a hierarchical one, you need some kind of parent child relation.

            How shall a proxymodel know about these relation?

            Regards
            Karl-Heinz

            ProgSysP 1 Reply Last reply
            0
            • K karlheinzreichel

              @ProgSys I want to dynamically convert any given flat model into a hierarchical one.

              But when you will convert ANY given flat model into a hierarchical one, you need some kind of parent child relation.

              How shall a proxymodel know about these relation?

              Regards
              Karl-Heinz

              ProgSysP Offline
              ProgSysP Offline
              ProgSys
              wrote on last edited by
              #6

              @VRonin said in Convert list into a Tree Model by role:

              This is hard but the opposite (i.e. tree to list) is trivial. any chance you can structure your original model as a tree instead and convert it to a list later on?

              @VRonin Possible it is, but if you have a table with many columns, then you would need to create and manage trees for each column, if you wish to display the same data simultaneously in different tree vews. My hope was I could separate the data and the views.

              @karlheinzreichel said in Convert list into a Tree Model by role:

              @ProgSys I want to dynamically convert any given flat model into a hierarchical one.

              But when you will convert ANY given flat model into a hierarchical one, you need some kind of parent child relation.

              How shall a proxymodel know about these relation?

              Regards
              Karl-Heinz

              @karlheinzreichel I was hoping based on a role or column, but you are right, I do not know how.

              I guess, I'll just create trees that will reference the original flat data and use them to create tree models.

              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