Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Regarding traversing QList

    General and Desktop
    5
    10
    6248
    Loading More Posts
    • 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
      Indrajeet last edited by

      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 Reply Quote 0
      • Zlatomir
        Zlatomir last edited by

        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 Reply Quote 0
        • A
          andre last edited by

          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 Reply Quote 0
          • I
            Indrajeet last edited by

            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 Reply Quote 0
            • D
              DenisKormalev last edited by

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

              1 Reply Last reply Reply Quote 0
              • Zlatomir
                Zlatomir last edited by

                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 Reply Quote 0
                • A
                  andre last edited by

                  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 Reply Quote 0
                  • Zlatomir
                    Zlatomir last edited by

                    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 Reply Quote 0
                    • M
                      maximus last edited by

                      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 Reply Quote 0
                      • A
                        andre last edited by

                        [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 Reply Quote 0
                        • First post
                          Last post