Results 1 to 3 of 3

Thread: QTreeView adding children to the view

  1. #1
    Join Date
    Jul 2007
    Posts
    26
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default QTreeView adding children to the view

    Hello there.

    I have a QTreeView and am using a QStandardItemModel to fill that tree view. I allready have a constructed read/write list of data but would need to implement adding children to particular items as well.
    I tried a few ways, but just can't get it.

    This is how I'm adding items:
    Qt Code:
    1.  
    2. model->insertRow(itemCount, QModelIndex());
    3. pI = model->index(itemCount,0,QModelIndex());
    4. model->setData( pI, title, Qt::DisplayRole);
    5. model->item(itemCount, 0)->setEditable( false );
    To copy to clipboard, switch view to plain text mode 

    model is a QStandardItemModel pointer and title is a QString variable.

    And this is how I tried to add a child to only one item:
    Qt Code:
    1.  
    2. model->insertRow(1, QModelIndex());
    3. cI = model->index(1, 0, QModelIndex());
    4. model->setData( cI, "test", Qt::DisplayRole);
    5. model->item(1, 0)->setChild(0, 1, model->item(5,0));
    To copy to clipboard, switch view to plain text mode 
    The list is filled with data and then this piece of code get's executed.
    But what happens is that another item is added("test") and an empty child is assigned to it. But I would like to assign this "test" to an existing item on second row in the list.

    I did read the docs up and down but can't figure it out.

  2. #2
    Join Date
    Jul 2007
    Posts
    26
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView adding children to the view

    Anybody? :/
    Looking for lots of help with my project.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeView adding children to the view

    Perhaps you want something like this:
    Qt Code:
    1. QStandardItem* parent = model->item(row);
    2. if (parent)
    3. {
    4. QStandardItem* child = new QStandardItem("test");
    5. parent->appendRow(child);
    6. }
    To copy to clipboard, switch view to plain text mode 
    You can also pick the parent like this (if you want to add a child under current index at runtime):
    Qt Code:
    1. QStandardItem* parent = model->itemFromIndex(view->currentIndex());
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

Similar Threads

  1. QTreeView questions about view doubles and more
    By davisjamesf in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2007, 00:39
  2. Adding Rectangular overlay on graphics view
    By forrestfsu in forum Qt Programming
    Replies: 10
    Last Post: 21st November 2006, 19:42
  3. QTreeView and QStandardModel : add children
    By Valheru in forum Newbie
    Replies: 7
    Last Post: 19th September 2006, 17:24

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.