Classes - Annotated - Tree - Functions - Home - Structure

QPtrListIterator Class Reference

The QPtrListIterator class provides an iterator for QPtrList collections. More...

#include <qptrlist.h>

Inherited by QStrListIterator.

List of all member functions.

Public Members


Detailed Description

The QPtrListIterator class provides an iterator for QPtrList collections.

Define a template instance QPtrListIterator<X> to create a list iterator that operates on QPtrList<X> (list of X*).

Example:

    #include <qptrlist.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;
    };

    int main()
    {
        QPtrList<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) );

        QPtrListIterator<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() );
        }
        return 0;
    }
  

Program output:

        Bill earns 50000
        Steve earns 80000
        Ron earns 60000
  

Although QPtrList 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 QPtrList'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 QPtrList knows about all list iterators that are operating on the list. When an item is removed from the list, the list updates all iterators pointing the removed item to point to the new current list item.

Example:

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

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

    int main()
    {
        QPtrList<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) );

        QPtrListIterator<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() );

        return 0;
    }
  

Program output:

        Ron
  

See also QPtrList and collection classes.


Member Function Documentation

QPtrListIterator::QPtrListIterator ( const QPtrList<type> & list )

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

QPtrListIterator::~QPtrListIterator ()

Destroys the iterator.

bool QPtrListIterator::atFirst () const

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

See also toFirst() and atLast().

bool QPtrListIterator::atLast () const

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

See also toLast() and atFirst().

uint QPtrListIterator::count () const

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

See also isEmpty().

Example: customlayout/card.cpp.

type * QPtrListIterator::current () const

Returns a pointer to the current iterator item.

Examples: customlayout/card.cpp and customlayout/flow.cpp.

bool QPtrListIterator::isEmpty () const

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

See also count().

QPtrListIterator::operator type * () const

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

type * QPtrListIterator::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 * QPtrListIterator::operator* ()

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

type * QPtrListIterator::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 * QPtrListIterator::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 * QPtrListIterator::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 * QPtrListIterator::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.

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

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

type * QPtrListIterator::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 * QPtrListIterator::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-2001 Trolltech, all rights reserved.


Copyright © 2001 TrolltechTrademarks
Qt version 3.0.0-beta2