| Classes - Annotated - Tree - Functions - Home - Structure

# QListIterator Class Reference

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

`#include <qlist.h>`

Inherits QGListIterator.

Inherited by QStrListIterator.

List of all member functions.

## Public Members

- QListIterator ( const QList<type> & list )
- ~QListIterator ()
- uint count () const
- bool isEmpty () const
- bool atFirst () const
- bool atLast () const
- type* toFirst ()
- type* toLast ()
- operator type* ()const
- type* operator* ()
- type* current () const
- type* operator ()
- type* operator++ ()
- type* operator+= ( uint jump )
- type* operator-- ()
- type* operator-= ( uint jump )
- QListIterator<type>& operator= ( const QListIterator<type> & it )

## Detailed Description

The QListIterator class provides an iterator for QList collections.

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().latin1(), emp->salary() );
            }
        }

Program output:

            Bill earns 50000
            Steve earns 80000
            Ron earns 60000

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().latin1() );
        }

Program output:

            Ron

See also QList and collection classes

Examples: dirview/main.cpp

## 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* ()

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

### 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.trolltech.com):

This file is part of the Qt toolkit, copyright © 1995-2000 Trolltech, all rights reserved.