Regarding traversing QList



  • 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.



  • 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



  • 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) {}


  • 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



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



  • 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.



  • 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);
    @



  • 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.



  • 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 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.