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. Regarding traversing QList

Regarding traversing QList

Scheduled Pinned Locked Moved General and Desktop
10 Posts 5 Posters 6.9k 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.
  • I Offline
    I Offline
    Indrajeet
    wrote on last edited by
    #1

    Hi All

    I have written following code for traversing QList
    @
    QList<Employee*> list;

        list.append(new Employee("Bill", 50000) );
        list.append(new Employee("Steve",80000) );
        list.append(new Employee("Ron",  60000) );
    
        Employee *emp;
        for ( emp = list.first(); emp != 0; emp = list.next() )
            printf( "%s earns %d\n", emp->name(), emp->salary() );
    

    @

    The problem I am facing is in for loop i.e QList dont have "next" as its member function so
    I am not getting how to move to next object in QList.

    1 Reply Last reply
    0
    • ZlatomirZ Offline
      ZlatomirZ Offline
      Zlatomir
      wrote on last edited by
      #2

      QList doesn't have next because it is not a linked list, see "here":http://doc.qt.nokia.com/latest/qlist.html#details
      //use iterators to traverse the QList or indexes to directly access an element

      https://forum.qt.io/category/41/romanian

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        Indeed, as Zlatomir sais, there are other ways to traverse QList. QList does not have a concept of a "current item", so there also are no next or previous methods on the QList. Also, there are no such methods on the elements of course either, as those can be anything.

        Instead, you can use:

        • Iterators, or
        • A loop that directly accesses each element via its index: for (int i(0); i<list.count(); ++i) {}, or
        • foreach (Employee *employee, list) {}
        1 Reply Last reply
        0
        • I Offline
          I Offline
          Indrajeet
          wrote on last edited by
          #4

          Hi Andre

          I did it the way you suggested

          @ QList<Employee*> list; // list of pointers to Employee

              list.append(new Employee("Bill", 50000) );
              list.append(new Employee("Steve",80000) );
              list.append(new Employee("Ron",  60000) );
             
              for (int i = 0; i < list.size(); ++i)
          

          {
          Employee *emp;
          emp = list.at(i);
          qDebug("%s earns %d\n",emp->name().toAscii().data(),emp->salary());
          }@

          Now i have one doubt how to safely delete all the allocated objects

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DenisKormalev
            wrote on last edited by
            #5

            @
            qDeleteAll(list);
            list.clear();
            @
            will delete all objects in your list and clear it

            1 Reply Last reply
            0
            • ZlatomirZ Offline
              ZlatomirZ Offline
              Zlatomir
              wrote on last edited by
              #6

              Careful with containers of pointers, if you remove an element (pointer) from the container you need to call delete (de-allocate the object) before the actual remove (remove the pointer from the container) - else you have a leak.

              So: better use containers of objects or if the objects are "hard" to copy you can use containers of "shared_pointers":http://doc.qt.nokia.com/4.7-snapshot/qsharedpointer.html#details instead of "normal" pointers.

              https://forum.qt.io/category/41/romanian

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                It is not as dramatic as Zlatomir claims. In fact, the solution Denis presents is perfectly save.

                Removing a single item from the can also be done like this:
                @
                delete list.takeAt(n); //n is a valid list index.

                //or, in two steps:
                Employee* emp = list.at(n);
                //order of the operations below does not matter
                delete emp;
                list.removeAt(n);
                @

                1 Reply Last reply
                0
                • ZlatomirZ Offline
                  ZlatomirZ Offline
                  Zlatomir
                  wrote on last edited by
                  #8

                  Andre, i didn't said it is dramatic ;) - i only said he should be careful to call delete - and also offered an elegant alternative solution.

                  https://forum.qt.io/category/41/romanian

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    maximus
                    wrote on last edited by
                    #9

                    Hi I came across this post looking for a good way to clear a QList of pointers.

                    I think it is now possible to do :
                    @ qDeleteAll(lst);
                    lst.clear();@

                    lst being a QList of any pointer


                    Free Indoor Cycling Software - https://maximumtrainer.com

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      [quote author="maximus" date="1396043243"]Hi I came across this post looking for a good way to clear a QList of pointers.

                      I think it is now possible to do :
                      @ qDeleteAll(lst);
                      lst.clear();@

                      lst being a QList of any pointer
                      [/quote]

                      That's exactly what was "said earlier":/forums/viewreply/47399/. This is not a new feature, it has been there for ages.

                      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