Qt logo

QListIterator Class Reference


The QListIterator class provides an iterator for QList collections. More...

#include <qlist.h>

Inherits QGListIterator.

List of all member functions.

Public Members


Detailed Description

The QListIterator class provides an iterator for QList collections.

QListIterator is implemented as both a template and a macro class. Define a template instance QListIterator<X> to create a list iterator that operates on QList<X> (list of X*).

Example:

    #include <qlist.h>
    #include <qstring.h>
    #include <stdio.h>

    class Employee
    {
    public:
        Employee( const char *name, int salary ) { n=name; s=salary; }
        const char *name()   const               { return n; }
        int         salary() const               { return s; }
    private:
        QString     n;
        int         s;
    };

    void main()
    {
        QList<Employee> list;             // list of pointers to Employee
        list.setAutoDelete( TRUE );       // delete items when they are removed

        list.append( new Employee("Bill", 50000) );
        list.append( new Employee("Steve",80000) );
        list.append( new Employee("Ron",  60000) );

        QListIterator<Employee> it(list); // iterator for employee list
        for ( ; it.current(); ++it ) {
            Employee *emp = it.current();
            printf( "%s earns %d\n", emp->name(), emp->salary() );
        }
    }

Program output:

        Bill earns 50000
        Steve earns 80000
        Ron earns 60000

A QListIterator can also be instantiated through a macro expansion, but this is necessary only for compilers that do not support templates. See the collection classes documentation for a general discussion on template-based versus macro-based collections.

Although QList has member functions to traverse the doubly linked list structure, using a list iterator is a much more robust way of traversing the list, because multiple list iterators can operate on the same list, independent of each other and independent of the QList's current item. An iterator has its own current list item and can get the next and previous list items. It can only traverse the list, never modify it.

A QList knows about all list iterators that are operating on the list. When an item is removed from the list, the list update all iterators that are pointing the removed item to point to the new current list item.

Example:

    #include <qlist.h>
    #include <qstring.h>
    #include <stdio.h>

    class Employee
    {
        ...     // same as above
    };

    void main()
    {
        QList<Employee> list;             // list of pointers to Employee
        list.setAutoDelete( TRUE );       // delete items when they are removed

        list.append( new Employee("Bill", 50000) );
        list.append( new Employee("Steve",80000) );
        list.append( new Employee("Ron",  60000) );

        QListIterator<Employee> it(list);

        list.at( 1 );                     // current list item: "Steve"
        it.toLast();                      // it: "Ron"
        --it;                             // it: "Steve"

          // Now, both the list and the iterator are referring the same item

        list.remove();
        printf( "%s\n", it.current()->name() );
    }

Program output:

        Ron

See also: QList and collection classes


Member Function Documentation

QListIterator::QListIterator ( const QList<type> & list )

Constructs an iterator for list. The current iterator item is set to point on the first item in the list.

QListIterator::~QListIterator ()

Destroys the iterator.

QListIterator::operator type * () const

Cast operator. Returns a pointer to the current iterator item. Same as current().

bool QListIterator::atFirst () const

Returns TRUE if the current iterator item is the first list item, otherwise FALSE.

See also: toFirst() and atLast().

bool QListIterator::atLast () const

Returns TRUE if the current iterator item is the last list item, otherwise FALSE.

See also: toLast() and atFirst().

uint QListIterator::count () const

Returns the number of items in the list this iterator operates on.

See also: isEmpty().

type * QListIterator::current () const

Returns a pointer to the current iterator item.

bool QListIterator::isEmpty () const

Returns TRUE if the list is empty, i.e. count() == 0, otherwise FALSE.

See also: count().

type * QListIterator::operator() ()

Makes the succeeding item current and returns the original current item.

If the current iterator item was the last item in the list or if it was null, null is returned.

type * QListIterator::operator++ ()

Prefix ++ makes the succeeding item current and returns the new current item.

If the current iterator item was the last item in the list or if it was null, null is returned.

type * QListIterator::operator+= ( uint jump )

Sets the current item to the item jump positions after the current item, and returns a pointer to that item.

If that item is beyond the last item or if the dictionary is empty, it sets the current item to null and returns null.

type * QListIterator::operator-- ()

Prefix -- makes the preceding item current and returns the new current item.

If the current iterator item was the first item in the list or if it was null, null is returned.

type * QListIterator::operator-= ( uint jump )

Returns the item jump positions before the current item, or null if it is beyond the first item. Makes this the current item.

QListIterator<type>& QListIterator::operator= ( const QListIterator<type> & it )

Assignment. Makes a copy of the iterator it and returns a reference to this iterator.

type * QListIterator::toFirst ()

Sets the current iterator item to point to the first list item and returns a pointer to the item. Sets the current item to null and returns null if the list is empty.

See also: toLast() and atFirst().

type * QListIterator::toLast ()

Sets the current iterator item to point to the last list item and returns a pointer to the item. Sets the current item to null and returns null if the list is empty.

See also: toFirst() and atLast().


Search the documentation, FAQ, qt-interest archive and more (uses www.troll.no):


This file is part of the Qt toolkit, copyright © 1995-98 Troll Tech, all rights reserved.

It was generated from the following files:


Copyright © 1998 Troll TechTrademarks
Qt version 1.42