Convert list into a Tree Model by role



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





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



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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.