Results 1 to 9 of 9

Thread: Tree model implementation for C++ data to QML

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,327
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Tree model implementation for C++ data to QML

    I have found that it is best to -not- try to convert a hierarchical data structure directly into a hierarchical QAbstractItemModel (if I interpret your statement "making the Classroom derive from QAbstractItemModel" correctly).

    Instead derive a "ClassroomModel" (say) from QAbstractItemModel and store a pointer to your Classroom singleton in it as a data member. Then, you can devise a way to store something in each QModelIndex that uniquely identifies where that index is in the hierarchy. It could be an integer that represents where that node is in a traversal through the hierarchy (0 is the Classroom node, 1 is the first Group node, 2 is the first Person node in the first Group, 3 is the second Person node in that Group, etc.) It could be a void pointer to the Group or Person node. Anything that lets you map from a QModelIndex into your actual data structure.

    In any case, I think you will find the need for a back-pointer from Person to Group or "groupId" members in Person and Group so you can tell which Group a Person belongs to. You will need this in order to implement the model's parent() method.

    The point of roles is so that a view can interrogate the model about how to customize the display of each member of the model. The only one you truly need to implement is the DisplayRole, since that provides the string that is used to display that item in the view. But there are many other roles which can be used to customize the appearance and which the view will ask for as it draws each item. You could have icons that are different for Classroom, Group, and Person nodes, different text or background colors, something other than the default left alignments, etc.

    Identity proxies are an interesting way to display the same base model in different ways in different views by using a different identity model for each view. It is in the identity model where the "decorations" get added (for icon, color, alignment, etc. roles) and the base model simply satisfies the DisplayRole.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. The following user says thank you to d_stranz for this useful post:

    xconverge (24th January 2021)

Similar Threads

  1. Replies: 3
    Last Post: 17th January 2016, 18:06
  2. Replies: 3
    Last Post: 16th November 2015, 21:22
  3. How to map tree model data to list view
    By msopanen in forum Qt Programming
    Replies: 0
    Last Post: 10th November 2009, 19:56
  4. Replies: 1
    Last Post: 7th July 2009, 07:13
  5. data, model and tree view
    By larry104 in forum Qt Programming
    Replies: 17
    Last Post: 3rd July 2006, 14:43

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
  •  
Qt is a trademark of The Qt Company.