Results 1 to 5 of 5

Thread: Deleting rows (children) from a qstandarditem

  1. #1
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Deleting rows (children) from a qstandarditem

    I have a bit of a strange problem here. With the following code :

    Qt Code:
    1. int rowCount = model->item( row )->rowCount();
    2. for( int i = 0; i < rowCount; ++i ){
    3. QList< QStandardItem* > items = model->item( row )->takeRow( i );
    4. qDeleteAll( items );
    5. }
    To copy to clipboard, switch view to plain text mode 

    every SECOND row gets deleted. I'm a bit stumped, since a quick qDebug() shows that rowCount is indeed being set to the amount of rows in the QStandardItem. And since i is only being incremented by 1 every iteration, it SHOULD delete one row every time, and not skip a row. Is this a bug in Qt4? I'm running qt-copy BTW, the patched KDE4 snapshot.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Deleting rows (children) from a qstandarditem

    Use a while loop instead. Like this:

    Qt Code:
    1. while(model->item( row )->rowCount() ){
    2. QList< QStandardItem* > items = model->item( row )->takeRow( 0 );
    3. qDeleteAll( items );
    4. }
    To copy to clipboard, switch view to plain text mode 

    I'm not sure if it works, but give it a try .

    Regards

  3. #3
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Deleting rows (children) from a qstandarditem

    Yes, I just realised what a silly mistake I was making

  4. #4
    Join Date
    Mar 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Deleting rows (children) from a qstandarditem

    Thank you guys, googling around I found this thread very useful.
    But in my situation some of the child items may have in turn a set of children, and my guess is I am introducing some memory leak each time I delete all the children of parent without some recursion on the grand-children. Am I wrong?

    The implementation suggested above works fine, but when I try it with a kind of recursion I get an application crash.

    Qt Code:
    1. QStandardItem * loopItem = myParentItem;
    2. while (loopItem->rowCount())
    3. { QList<QStandardItem *> items = loopItem->takeRow(0);
    4. if (items.at(0)->hasChildren()) loopItem = items.at(0);
    5. qDeleteAll(items);
    6. }
    To copy to clipboard, switch view to plain text mode 

    someone have any suggestion?
    tnx,
    FMG
    Last edited by FMG; 29th March 2012 at 09:07.

  5. #5
    Join Date
    Mar 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Deleting rows (children) from a qstandarditem

    ... I got the point, obviously I can't delete an object and use it later... anyway if it can be of some interest toanyone, this is the code of the working function that deletes all the children of a QStandardItem and recusrively all the "younger" relatives, avoiding a memory leak.

    Qt Code:
    1. //delete all children of parent;
    2. QStandardItem * loopItem = parent; //main loop item
    3. QList<QStandardItem *> carryItems; //Last In First Out stack of items
    4. QList<QStandardItem *> itemsToBeDeleted; //List of items to be deleted
    5. while (loopItem->rowCount())
    6. {
    7. itemsToBeDeleted << loopItem->takeRow(0);
    8. //if the row removed has children:
    9. if (itemsToBeDeleted.at(0)->hasChildren())
    10. {
    11. carryItems << loopItem; //put on the stack the current loopItem
    12. loopItem = itemsToBeDeleted.at(0); //set the row with children as the loopItem
    13. }
    14. //if current loopItem has no more rows but carryItems list is not empty:
    15. if (!loopItem->rowCount() && !carryItems.isEmpty()) loopItem = carryItems.takeFirst();
    16.  
    17. }
    18. qDeleteAll(itemsToBeDeleted);
    To copy to clipboard, switch view to plain text mode 

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.