Qt World Summit: Register Today!

Proper way to remove items from a QListWidget[Solved]

  • 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");



    and trying to remove some items in a slot like this:


    for(int i=2;i<mListWidget->count();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.

  • Lifetime Qt Champion


    mListWidget->count() will not return the same value each time you do an iteration since you are removing items while also counting.

  • Thanks,SGaist,I really have had a long day!!!

  • Sorry to bother again.This should solve the problem right?

    int numberOfElements=mListWidget->count();

     for(int i=2;i<numberOfElements;i++)


    but I am still getting the same results as before.What is going on .....??

  • 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.

  • Lifetime Qt Champion

    To add to 1+1=2, your list size still changes, so your counter is going to grow past the size of your list

  • General trick in deleting items from a list while iteration over that list, is to iterate backwards. That will make your life much easier...

  • Does the takeItem() take care of freeing the memory allocate with new operator ?

  • Lifetime Qt Champion

    It's explained in the "documentation":http://qt-project.org/doc/qt-4.8/qlistwidget.html#takeItem

  • Thank you all guys ,was ignoring that the list changed sizes.Its OK now.

  • 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.

  • 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++)


    It works wonders.

  • Lifetime Qt Champion


    while (!mListWidget.isEmpty()) {
    QListWidgetItem *item ' mListWidget->takeItem();
    delete item;

Log in to reply