Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Proper way to remove items from a QListWidget[Solved]
QtWS25 Last Chance

Proper way to remove items from a QListWidget[Solved]

Scheduled Pinned Locked Moved General and Desktop
13 Posts 6 Posters 15.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    musimbate
    wrote on 30 Jul 2013, 09:03 last edited by
    #1

    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.

    Why join the navy if you can be a pirate?-Steve Jobs

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 30 Jul 2013, 09:08 last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        musimbate
        wrote on 30 Jul 2013, 09:11 last edited by
        #3

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

        Why join the navy if you can be a pirate?-Steve Jobs

        1 Reply Last reply
        0
        • M Offline
          M Offline
          musimbate
          wrote on 30 Jul 2013, 09:24 last edited by
          #4

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

          Why join the navy if you can be a pirate?-Steve Jobs

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dbzhang800
            wrote on 30 Jul 2013, 09:51 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 30 Jul 2013, 11:14 last edited by
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on 30 Jul 2013, 13:10 last edited by
                #7

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

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  john_god
                  wrote on 30 Jul 2013, 13:39 last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 30 Jul 2013, 15:15 last edited by
                    #9

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

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      musimbate
                      wrote on 31 Jul 2013, 04:05 last edited by
                      #10

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

                      Why join the navy if you can be a pirate?-Steve Jobs

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        pwnstar23
                        wrote on 31 Jul 2013, 04:19 last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          musimbate
                          wrote on 31 Jul 2013, 05:03 last edited by
                          #12

                          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.

                          Why join the navy if you can be a pirate?-Steve Jobs

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 31 Jul 2013, 07:27 last edited by
                            #13

                            Rather

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

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0

                            6/13

                            30 Jul 2013, 11:14

                            • Login

                            • Login or register to search.
                            6 out of 13
                            • First post
                              6/13
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved