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. QList removing via iterator problem
Qt 6.11 is out! See what's new in the release blog

QList removing via iterator problem

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 13.4k 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.
  • F Offline
    F Offline
    fluca1978
    wrote on last edited by
    #1

    Hi all,
    I've got a QList of objects named "Role", and I want to remove some of them while iterating the list. The first piece of code I wrote was:

    @
    QList<Role>::iterator roleIterator;
    for( roleIterator = roles.begin(); roleIterator != roles.end(); roleIterator++ ){
    const Role& currentRole = *roleIterator;

    if( ! currentRole.rootLevelRole() ){
        roleIterator = roles.erase( roleIterator );
    }
    

    }
    @

    but it was not working, that is it what not removing all the items. I noted that it was skipping the next-to-last-erased item, so I changed the code into

    @
    QList<Role>::iterator roleIterator;
    for( roleIterator = roles.begin(); roleIterator != roles.end(); roleIterator++ ){
    const Role& currentRole = *roleIterator;

    if( ! currentRole.rootLevelRole() ){
      roles.removeOne( currentRole );
    }
    

    }
    @

    that worked. Now I'm confused about the usage of "erase":http://qt-project.org/doc/qt-4.8/qlist.html#erase : am I not supposed to re-assign the iterator once it has been erased?

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

      Have a look at "this thread":https://qt-project.org/forums/viewthread/13816/.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        task_struct
        wrote on last edited by
        #3

        Also you can read "this article":https://qt-project.org/wiki/QtIterators

        "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

        • Linu...
        1 Reply Last reply
        0
        • F Offline
          F Offline
          fluca1978
          wrote on last edited by
          #4

          Sorry, not sure to get it right, but the problem is the roleIterator++ which increments the iterator that has already advanced by the erase method call. Is it right? Therefore this works:

          @
          QList<Role>::iterator roleIterator = roles.begin();
          while( roleIterator != roles.end() ){
          const Role& currentRole = *roleIterator;

              if( ! currentRole.rootLevelRole() ){
                  roleIterator = roles.erase( roleIterator );
              }
               else
                  roleIterator++;
            }
          

          @

          Am I right?

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

            Correct.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Aksh
              wrote on last edited by
              #6

              Hii friends...

              I am facing problem while deleting item from QList.
              is it any difference while deleting item from QList of pointer type elements and deleting item from QList of elements(non pointer)????

              QList<Myclass*> MyList; ////////////this is list having 3 items

              //trying to delete item having name "abc"

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

                the difference is that in order to avoid memory leaks you need to delete pointers and only need to remove the objects allocated on the stack.

                1 Reply Last reply
                0
                • JeroentjehomeJ Offline
                  JeroentjehomeJ Offline
                  Jeroentjehome
                  wrote on last edited by
                  #8

                  If I'm not mistaken, the QList takes ownership of the inserted objects, so when deleting a "pointer" from the list, it will automatic delete the allocated data (if no other references are present).
                  Greetz

                  Greetz, Jeroen

                  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