Results 1 to 3 of 3

Thread: How to add child items to QStandardItemModel?

  1. #1
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default How to add child items to QStandardItemModel?

    Greetings,

    Idea is to have something like:

    > File
    .........New
    .........Save
    >Edit
    .........Cut


    I have:

    Qt Code:
    1. int main( int argc, char *argv[] )
    2. {
    3. .........
    4.  
    5.  
    6. QStandardItem *parentItem0 = model.invisibleRootItem();
    7.  
    8.  
    9. QStandardItem *file_item = new QStandardItem(QString("File"));
    10.  
    11.  
    12. QStandardItem *new_ = new QStandardItem(QString("New"));
    13.  
    14. file_item->setChild(1, new_);
    15.  
    16.  
    17. parentItem0 = file_item;
    18.  
    19.  
    20. parentItem0->appendRow( file_item );
    21.  
    22. ..........
    23. }
    To copy to clipboard, switch view to plain text mode 
    This is not giving me anything.
    What is the way to add child items to parent items?

  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: How to add child items to QStandardItemModel?

    From the QStandardItemModel docs:

    An example usage of QStandardItemModel to create a tree:

    Qt Code:
    1. QStandardItem *parentItem = model.invisibleRootItem();
    2. for (int i = 0; i < 4; ++i) {
    3. QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
    4. parentItem->appendRow(item);
    5. parentItem = item;
    6. }
    To copy to clipboard, switch view to plain text mode 

    This code creates a tree that is 4 levels deep because each time though the loop the parent item pointer is replaced by the item just added:

    item 0 -> item 1 -> item 2 -> item 3

    This is basically what you want to do with your File -> New and File -> Save

    This is not giving me anything.
    Not surprising, because in Line 18 you reassign the parent item pointer to the file_item you just created, then tell that pointer to add itself as a child row of itself.

    What you code needs to do is this:

    0. Retrieve the invisible root item pointer.
    1. Create the "File" item QStandardItem
    2. Create the "New" item
    3. Add it to the File item using add row
    4. Create the "Save" item
    5. Add it to the File item using add row
    6. Add the File item to the root item using add row
    7. Create the "Edit" item
    8. Create the "Cut" item
    9. Add it to the Edit item
    10. Add the Edit item to the root item
    etc.

    You can change up the order of creation and addition of child items; that is, you can create the File item and add it to the root, then create the Edit item and add it to the root, then create the children of File and add them to the File item, etc. The important thing is to add each child to the correct parent, and never reassign the root pointer variable like you did in Line 18 of your code.
    <=== 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.

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

    TheIndependentAquarius (5th January 2021)

  4. #3
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: How to add child items to QStandardItemModel?

    Am thankful for your detailed response.

Similar Threads

  1. Replies: 0
    Last Post: 2nd July 2015, 03:47
  2. Getting Child Items from QStandardItemModel
    By nikhilqt in forum Qt Programming
    Replies: 4
    Last Post: 4th June 2013, 16:10
  3. Replies: 1
    Last Post: 14th January 2012, 01:33
  4. Replies: 0
    Last Post: 11th July 2011, 10:31
  5. Replies: 2
    Last Post: 23rd July 2008, 18:48

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.