Proper way to remove items from a QListWidget[Solved]
-
wrote on 30 Jul 2013, 09:03 last edited by
Hi ,
I am test-populating my ListWidget like this:@
QListWidgetItem * itemno0=new QListWidgetItem(QIcon("D:/QtProjects/modelViewProg/home.png"),"itemno0");
QListWidgetItem * itemno1=new QListWidgetItem(QIcon("D:/QtProjects/modelViewProg/home.png"),"itemno1");
QListWidgetItem * itemno2=new QListWidgetItem(QIcon("D:/QtProjects/modelViewProg/home.png"),"itemno2");
QListWidgetItem * itemno3=new QListWidgetItem(QIcon("D:/QtProjects/modelViewProg/home.png"),"itemno3");
QListWidgetItem * itemno4=new QListWidgetItem(QIcon("D:/QtProjects/modelViewProg/home.png"),"itemno4");
QListWidgetItem * itemno5=new QListWidgetItem(QIcon("D:/QtProjects/modelViewProg/home.png"),"itemno5");mListWidget->addItem(itemno0); mListWidget->addItem(itemno1); mListWidget->addItem(itemno2); mListWidget->addItem(itemno3); mListWidget->addItem(itemno4); mListWidget->addItem(itemno5);
@
and trying to remove some items in a slot like this:
@
for(int i=2;i<mListWidget->count();i++)
mListWidget->takeItem(i);@
but I am puzzled by the fact that I am getting only items 2 and 4 removed and the others stay.I expected only itemno0 and itemno1 to stay.
Does this have something to do with the inner workings of QListWidget or there is something I am getting wrong here.
Any help would be appreciated.Thanks. -
Hi,
mListWidget->count() will not return the same value each time you do an iteration since you are removing items while also counting.
-
wrote on 30 Jul 2013, 09:11 last edited by
Thanks,SGaist,I really have had a long day!!!
-
wrote on 30 Jul 2013, 09:24 last edited by
Sorry to bother again.This should solve the problem right?
@int numberOfElements=mListWidget->count();
for(int i=2;i<numberOfElements;i++) mListWidget->takeItem(i);
@
but I am still getting the same results as before.What is going on .....??
-
wrote on 30 Jul 2013, 09:51 last edited by
No, your usage is still wrong.
You should always remove the second item, until there is no second item any more, which mean you have only one item now!
or you should remove your item from the last to the second item.
-
To add to 1+1=2, your list size still changes, so your counter is going to grow past the size of your list
-
wrote on 30 Jul 2013, 13:10 last edited by
General trick in deleting items from a list while iteration over that list, is to iterate backwards. That will make your life much easier...
-
wrote on 30 Jul 2013, 13:39 last edited by
Does the takeItem() take care of freeing the memory allocate with new operator ?
-
It's explained in the "documentation":http://qt-project.org/doc/qt-4.8/qlistwidget.html#takeItem
-
wrote on 31 Jul 2013, 04:05 last edited by
Thank you all guys ,was ignoring that the list changed sizes.Its OK now.
-
wrote on 31 Jul 2013, 04:19 last edited by
If you call takeItem you still need to call delete on it, a better way is to just loop over the items and call delete on them without calling takeItem, it works ok because the dtor will remove itself from the ListWidget during destruction.
-
wrote on 31 Jul 2013, 05:03 last edited by
Thanks pwnstar23,
so what you mean is something like this right?
@//count is the number of items to delete
for(int i=0;i<count;i++)
delete(mListWidget->takeItem(indexLocal+1));@
It works wonders.
-
Rather
@
while (!mListWidget.isEmpty()) {
QListWidgetItem *item ' mListWidget->takeItem();
delete item;
}
@
6/13