Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Proper way to remove items from a QListWidget[Solved]
Qt 6.12 LTS is out! See what's new in the release blog

Proper way to remove items from a QListWidget[Solved]

Scheduled Pinned Locked Moved General and Desktop
13 Posts 6 Posters 16.6k Views 1 Watching
  • 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.
  • musimbateM Offline
    musimbateM Offline
    musimbate
    wrote on 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
    • musimbateM Offline
      musimbateM Offline
      musimbate
      wrote on 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 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
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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 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 last edited by
              #8

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

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 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
                • musimbateM Offline
                  musimbateM Offline
                  musimbate
                  wrote on 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
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on 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
                    • musimbateM Offline
                      musimbateM Offline
                      musimbate
                      wrote on 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
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 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

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt
                        • Unsolved