Results 1 to 5 of 5

Thread: Deriving QAbstractProxyModel : table to tree

  1. #1
    Join Date
    Feb 2013
    Posts
    38
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Deriving QAbstractProxyModel : table to tree

    Hi,

    I'd like to make a tree from a table. How to reimplement the map functions ?

    I see this question a lot but no answer. I would be okay with an example.

    Thanks,

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Deriving QAbstractProxyModel : table to tree

    Making a tree from a table makes no sense. Trees are hierarchical, with a root node (usually invisible), top level nodes, children of the top level nodes, and so forth. Tables are flat - every row in the table would become a top-level tree node with no children below them.

    It is more common to want to flatten a tree into a table, by turning each leaf node into a row, with columns containing the repeating parent items. There is a kptflatproxymodel class in KDE that does exactly this.

    Maybe you need to explain some more what exactly you want your tree to look like, and maybe that will help you understand what the map functions should do.

  3. #3
    Join Date
    Feb 2013
    Posts
    38
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Deriving QAbstractProxyModel : table to tree

    Hi,

    What I am trying to achieve is basic (I think, still can't do it...).

    Table :

    Qt Code:
    1. Id | n0 | n1
    2. 1 | 1 | 1
    3. 2 | 1 | 2
    4. 3 | 2 | 1
    5. 4 | 2 | 2
    To copy to clipboard, switch view to plain text mode 

    Expected tree with grouping by n0 :

    Qt Code:
    1. Id | n0 | n1
    2. + 1 | 1 | -
    3. +---1 | 1 | 1
    4. +---2 | 1 | 2
    5. + 3 | 2 | -
    6. +---3 | 2 | 1
    7. +---4 | 2 | 2
    To copy to clipboard, switch view to plain text mode 

    The table structure is a constraint.
    Last edited by moijhd; 15th October 2015 at 17:53.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Deriving QAbstractProxyModel : table to tree

    Your proxy needs to hold a tree structure of data that it can use to map between the source and its own indexes.

    If the depth is always just 2, then a list of nodes should be sufficient, where each node holds range of source rows belonging to a certain top level know (assuming n0 is sorted)

    Something like
    Qt Code:
    1. struct TopLevel {
    2. int beginRow;
    3. int endRow;
    4. };
    To copy to clipboard, switch view to plain text mode 

    It is best to do this on paper first.
    Your example would be something like a
    Qt Code:
    1. [(0, 1), (2, 3)]
    To copy to clipboard, switch view to plain text mode 
    Each top level index in our proxy refers to one of these structs, their children one entry per index in the struct's range.

    Cheers,
    _

  5. #5
    Join Date
    Feb 2013
    Posts
    38
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Deriving QAbstractProxyModel : table to tree

    Okay for the structure (I can't actually do it because I have millions of rows but let's forget about that for the moment).

    Now, if I am able to define which index (source or proxy) is top level, how do I map ie what should I return ?

    In the following code, we assume that the Qt::UserRole + i are well defined :

    Qt Code:
    1. ProxyModel::mapFromSource(
    2. const QModelIndex &sourceIndex
    3. ) const
    4. {
    5. // Check if the index is valid
    6. if(!sourceIndex.isValid())
    7. {
    8. // Not a valid index
    9. return QModelIndex();
    10. }
    11.  
    12. // Check if the index is a top level node in the proxy
    13. if(sourceIndex.data(Qt::UserRole).toBool())
    14. {
    15. // Row in the proxy
    16. int proxyRow;
    17.  
    18. // Get the row in the proxy
    19. proxyRow = sourceIndex.data(Qt::UserRole + 1);
    20.  
    21. // This is a top level node in the proxy
    22. return index(
    23. proxyRow,
    24. 0
    25. );
    26. }
    27. else
    28. {
    29. // This is a node under a top level node in the proxy
    30. // Let's ignore it for now
    31. return QModelIndex();
    32. }
    33. }
    34.  
    35. ProxyModel::mapToSource(
    36. const QModelIndex &proxyIndex
    37. ) const
    38. {
    39. // Check if the index is valid
    40. if(!proxyIndex.isValid())
    41. {
    42. // Not a valid index
    43. return QModelIndex();
    44. }
    45.  
    46. // Check if the index has a parent
    47. if(proxyIndex.parent() == QModelIndex())
    48. {
    49. // Row in the source
    50. int sourceRow;
    51.  
    52. // Get the row in the proxy
    53. sourceRow = proxyIndex.data(Qt::UserRole + 2);
    54.  
    55. // Top level node in the proxy
    56. return sourceModel()->index(
    57. sourceRow,
    58. 0
    59. );
    60. }
    61. else
    62. {
    63. // This is a node under a top level node in the proxy
    64. // Let's ignore it for now
    65. return QModelIndex();
    66. }
    67. }
    To copy to clipboard, switch view to plain text mode 

    Is this correct ?

Similar Threads

  1. how to make tree based table
    By gauravg in forum Qt Programming
    Replies: 1
    Last Post: 7th June 2012, 12:41
  2. Transparent QAbstractProxyModel for tree models
    By adzajac in forum Qt Programming
    Replies: 5
    Last Post: 27th October 2010, 11:03
  3. Replies: 0
    Last Post: 1st July 2010, 21:55
  4. Replies: 1
    Last Post: 11th June 2010, 08:14
  5. QAbstractProxyModel to do a Tree
    By xgoan in forum Qt Programming
    Replies: 3
    Last Post: 18th November 2008, 17:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.