Results 1 to 3 of 3

Thread: QStandardItemModel, parent / child to form tree structure

  1. #1
    Join Date
    May 2009
    Location
    Copenhagen
    Posts
    50
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QStandardItemModel, parent / child to form tree structure

    I'm getting a little stuck here guys!

    I'm using a QStandardItemModel (mymodel) for storing hierarchical data. At the point of filling the model with data I do not necessarily know the parent/child relationship in the dataset and the user need to be able to change that after data load. So I've divided the loading process in two steps. (1) Load the model from a database source and (2) create tree structure on the model.
    1. Load the model

      Qt Code:
      1. void MainWindow::LoadMyModel()
      2. {
      3. --Code snipped has been cut down to save space
      4. while (query.next())
      5. {
      6. for(int col=0; col < rec.count(); col++)
      7. {
      8. item->setData(query.value(col),Qt::DisplayRole);
      9. mymodel->setItem(row,col,item);
      10. }
      11. ++row;
      12. }
      13. }
      To copy to clipboard, switch view to plain text mode 
    2. Create tree structure
      Qt Code:
      1. void MainWindow::createtreestructure()
      2. {
      3. QStandardItem *parentItem = mymodel->invisibleRootItem(); //rootItem
      4. QStandardItem *oldparentItem ;//placeholder for Item->parent()
      5. for(int row = 0; row < mymodel->rowCount(); ++row ) {
      6. QStandardItem *item = mymodel->item(row,parent_acc_id);//Grap Item
      7. int parentid = item->data(Qt::DisplayRole).toInt();//this integer determines the (new) parent of Item
      8. oldparentItem = static_cast<QStandardItem *>(item->parent()); //cast parent in order to use QStandardItem::takeChild
      9. if(oldparentItem){
      10. oldparentItem->takeChild(item->row(),item->column());//remove (but do not delete) Item from model to avoid double entry
      11. }
      12. if( parentid >0 ) { //ParentKeys has been declared earlier as QMap<int,QStandardItem*> ParentKeys
      13. main->ParentKeys.value(parentid)->setChild(0,item); //Asign child to new parent
      14. } else {
      15. parentItem->setChild(0,item); //no parentid was found = assign to rootItem
      16. }
      17. }
      18. }
      To copy to clipboard, switch view to plain text mode 


    All this seems pretty straight forwarded but I'm simply not able to assign the QStandardItem to a new/other parent Item. I keep getting errors like 'QStandardItem::setChild: Ignoring duplicate insertion of item 0x94a6a70'.

    An other approach would be to subclass an abstractmodel (like in the simply treeview example shipped with Qt) to get the necessary functionality but it seems like QStandardItemModel has all the functionally required to create and maintain a tree structure.

    Initially it wouldn't be a problem to load the model and apply the tree structure from the beginning for instance by using the example provided by Qt...

    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 

    ...but I still need to be able to rearrange the parent/child relationship without going through the trouble of creating my own treemodel.

    Is there anyone who has experience with QStandardItemModel on how to assign parent/child relationship or how to rearrange parent/child after the model has been created? I'm running out of ideas!

    Thanks

  2. #2
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStandardItemModel, parent / child to form tree structure

    The problem may be that you are not removing the item when it has no parent. Try removing parentless items with mymodel->takeItem

  3. #3
    Join Date
    May 2009
    Location
    Copenhagen
    Posts
    50
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QStandardItemModel, parent / child to form tree structure

    An item is never parentless. You can't see it in the code snippet above but what Qt is doing behind the scenes is to assign items to the invisibleRootItem of the QStandardItemModel if it's not added to the model through another item (appendRow). That's what I'm trying to catch (see line 3 in the second code snippet). But I think you're right in the sense that it's the assigning/reassigning part that is the key to solving my problem.

Similar Threads

  1. Getting Child Items from QStandardItemModel
    By nikhilqt in forum Qt Programming
    Replies: 4
    Last Post: 4th June 2013, 16:10
  2. Tree structure
    By ikm in forum Newbie
    Replies: 1
    Last Post: 7th August 2009, 21:19
  3. Create Tree Structure
    By bismitapadhy in forum Qt Programming
    Replies: 4
    Last Post: 4th June 2009, 11:13
  4. Replies: 2
    Last Post: 23rd July 2008, 18:48
  5. Replies: 1
    Last Post: 28th July 2006, 15:10

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.