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. Problem using QList::RemoveAt function
Forum Updated to NodeBB v4.3 + New Features

Problem using QList::RemoveAt function

Scheduled Pinned Locked Moved General and Desktop
19 Posts 6 Posters 15.8k 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.
  • K Offline
    K Offline
    KA51O
    wrote on last edited by
    #8

    [quote author="koahnig" date="1327571283"][quote author="KA51O" date="1327570403"]I use this:

    @
    while(!list.isEmpty())
    {
    delete list.takeFirst();
    }
    @
    [/quote]

    The condition is missing in your case and
    @
    list.clear();
    @

    would be shorter and probably faster then. [/quote]

    actually list.clear() only removes the objects from the list. It does not delete the objects, which of course is fine if you still use them somewhere else.

    I was still editing the post to add a solution with a condition ^^

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #9

      [quote author="KA51O" date="1327571471"][quote author="koahnig" date="1327571283"][quote author="KA51O" date="1327570403"]I use this:

      @
      while(!list.isEmpty())
      {
      delete list.takeFirst();
      }
      @
      [/quote]

      The condition is missing in your case and
      @
      list.clear();
      @

      would be shorter and probably faster then. [/quote]

      actually list.clear() only removes the objects from the list. It does not delete the objects, which of course is fine if you still use them somewhere else.

      [/quote]

      Sorry, I did not read carefully enough :S I see now.

      Your approach would mean that you store pointers in the list. You delete the objects then, but this would be an endless loop.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • K Offline
        K Offline
        KA51O
        wrote on last edited by
        #10

        [quote author="koahnig" date="1327571719"][quote author="KA51O" date="1327571471"][quote author="koahnig" date="1327571283"][quote author="KA51O" date="1327570403"]I use this:

        @
        while(!list.isEmpty())
        {
        delete list.takeFirst();
        }
        @
        [/quote]

        The condition is missing in your case and
        @
        list.clear();
        @

        would be shorter and probably faster then. [/quote]

        actually list.clear() only removes the objects from the list. It does not delete the objects, which of course is fine if you still use them somewhere else.

        [/quote]

        Sorry, I did not read carefully enough :S I see now.

        Your approach would mean that you store pointers in the list. You delete the objects then, but this would be an endless loop. [/quote]

        No because QList::takeFirst() removes the first item and returns it at the same time.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KA51O
          wrote on last edited by
          #11

          this is Lukas Geyers solution with delete:

          @
          int i = 0;
          while(i < mylist.count())
          {
          if(condition)
          {
          YourType* objToDelete = mylist.at(i);
          mylist.removeAt(i);
          delete objToDelete;
          }
          else
          i++;
          }

          @

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #12

            [quote author="KA51O" date="1327571898"]
            No because QList::takeFirst() removes the first item and returns it at the same time.
            [/quote]
            Correct. Not my day :-(

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on last edited by
              #13

              [quote author="koahnig" date="1327571110"]I admit it is not the most elegant solution and I would vote for your while loop approach. Nevertheless, I do not agree that there be a problem as long as nobodyelse is fidling with the counter i.[/quote]
              Well, it looks like I agree with your disagreement (at least as long as it comes to counting down <code>for</code>) ;-)

              1 Reply Last reply
              0
              • I Offline
                I Offline
                Indrajeet
                wrote on last edited by
                #14

                Thanks Lukas While loop works correctly.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #15

                  [quote author="koahnig" date="1327568122"]Probably the simplist trick would be to go from the back

                  @
                  int itotal = mylist.count();
                  for(int i = itotal; i > 0; --i)
                  {
                  if(condition)
                  mylist.removeAt(i-1);
                  }
                  @

                  That would be my first check to get it work. I do not work with QList, but there might be better ways to do.

                  [/quote]

                  I regard the above solution as the most elegant and easy to read one, although I would modify and shorten it a bit more:

                  @
                  for(int i = mylist.count() - 1; i >= 0; --i)
                  {
                  if(condition_is_met)
                  delete mylist.takeAt(i);
                  }
                  @

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    Indrajeet
                    wrote on last edited by
                    #16

                    So while loop is best approach is it?

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on last edited by
                      #17

                      [quote author="Rajveer" date="1327573850"]So while loop is best approach is it?[/quote]

                      Use the solution that works for you and that you understand how it works.

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0
                      • _ Offline
                        _ Offline
                        _Michel
                        wrote on last edited by
                        #18

                        @
                        int i = 0;
                        // size() and count() are identical unless you give a paremeter to count(const T & value)
                        while (i < myList.size())
                        {
                        if (condition is met)
                        {
                        myList.removeAt(i); // "If you don't use the return value, removeAt() is more efficient.":http://qt-project.org/doc/qt-5/qlist.html#takeAt
                        }
                        else
                        {
                        i++;
                        }
                        }
                        @

                        1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          KA51O
                          wrote on last edited by
                          #19

                          But if you need to call delete then you need to use takeAt. Which is what was discussed above.
                          Anyways your code is just like the one posted by Lukas Geyer in the 3rd post.

                          [quote author="Lukas Geyer" date="1327568215"]@
                          int i = 0;
                          while(i < mylist.count())
                          {
                          if(condition)
                          mylist.removeAt(i);
                          else
                          i++;
                          }
                          @[/quote]

                          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 Extensions
                          • Unsolved