Results 1 to 6 of 6

Thread: Creating tree kind of model from QAbstractItemModel or QAbstractListModel

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    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: Creating tree kind of model from QAbstractItemModel or QAbstractListModel

    1. Do I need to derive my all classes system,SytemDatabase and CoresData from QAbstractItemModel .
    No. You probably do not have to change any of your existing classes. What you need is to derive a QAbstractItemModel that -uses- those classes (and the hierarchy you have built with them) to retrieve the information that will be displayed in your UI.

    2. Or Do i need to write like here in the example
    Also no, because your tree does not have a hierarchy like TreeItem -> TreeItem -> TreeItem... where all entries at any level of the tree are the same. In your case, you have System -> SystemDatabase -> CoresData. So, the "parent" item pointer for CoresData should point to the instance of SystemDatabase that owns it, SystemDatabase's parent is System, etc.

    And it makes no sense to have a CoresData:: appendChild() method, since those are the leaves of your tree and can't have children. Likewise, SystemDatabase:: appendChild() should add a CoresData child, not a SystemDatabase child. Same for the System class - it has SystemDatabase children.

    Your SystemTree class looks good, except I would add the System list as QList< System * > instead of QList< System >. If you define it as QList< System >, then the lifetime of the list entries is defined by the lifetime of the SystemTree - when the SystemTree goes out of scope, the QList and its contents will be deleted. If you define it as QList< System * >, then you can create and manage the System instances independently of the tree model. When the SystemTree goes out of scope, only the QList will be destroyed, not the pointers stored in it.
    <=== 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:

    gowreesh (14th January 2020)

Similar Threads

  1. Replies: 8
    Last Post: 11th March 2016, 09:56
  2. Replies: 4
    Last Post: 11th October 2012, 19:47
  3. Creating a Tree Model from a QStandardModel
    By kaushal_gaurav in forum Qt Programming
    Replies: 3
    Last Post: 19th December 2008, 06:47
  4. Help needed in creating Tree Model.
    By kaushal_gaurav in forum Qt Programming
    Replies: 5
    Last Post: 18th December 2008, 12:14
  5. Replies: 1
    Last Post: 6th July 2007, 12:16

Tags for this Thread

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.