Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# QPtrList Class Reference

The QPtrList class is a template class that provides doubly-linked lists. More...

`#include <qptrlist.h>`

Inherits QPtrCollection.

Inherited by QSortedList and QStrList.

List of all member functions.

## Public Members

- QPtrList ()
- QPtrList ( const QPtrList<type> & list )
- ~QPtrList ()
- QPtrList<type> & operator= ( const QPtrList<type> & list )
- bool operator== ( const QPtrList<type> & list ) const
- virtual uint count () const
- bool isEmpty () const
- bool insert ( uint index, const type * item )
- void inSort ( const type * item )
- void prepend ( const type * item )
- void append ( const type * item )
- bool remove ( uint index )
- bool remove ()
- bool remove ( const type * item )
- bool removeRef ( const type * item )
- void removeNode ( QLNode * node )
- bool removeFirst ()
- bool removeLast ()
- type * take ( uint index )
- type * take ()
- type * takeNode ( QLNode * node )
- virtual void clear ()
- void sort ()
- int find ( const type * item )
- int findNext ( const type * item )
- int findRef ( const type * item )
- int findNextRef ( const type * item )
- uint contains ( const type * item ) const
- uint containsRef ( const type * item ) const
- type * at ( uint index )
- int at () const
- type * current () const
- QLNode * currentNode () const
- type * getFirst () const
- type * getLast () const
- type * first ()
- type * last ()
- type * next ()
- type * prev ()
- void toVector ( QGVector * vec ) const

## Important Inherited Members

- bool autoDelete () const
- void setAutoDelete ( bool enable )

## Protected Members

- virtual int compareItems ( QPtrCollection::Item item1, QPtrCollection::Item item2 )
- virtual QDataStream & read ( QDataStream & s, QPtrCollection::Item & item )
- virtual QDataStream & write ( QDataStream & s, QPtrCollection::Item item ) const

## Detailed Description

The QPtrList class is a template class that provides doubly-linked lists.

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

The list class is indexable and has a current index and a current item. The first item corresponds to index 0. The current index is -1 if the current item is null.

Items are inserted with prepend(), insert() or append(). Items are removed with remove(), removeRef(), removeFirst() and removeLast(). You can search for an item using find(), findNext(), findRef() or findNextRef(). The list can be sorted with sort(). You can count the number of occurrences of an item with contains() or containsRef(). You can get a pointer to the current item with current(), to an item at a particular index position in the list with at() or to the first or last item with getFirst() and getLast(). You can also iterate over the list with first(), last(), next() and prev() (which all update current()). The list's deletion property is set with setAutoDelete().

Example:

        class Employee
        {
        public:
            Employee() : sn( 0 ) { }
            Employee( const QString& forename, const QString& surname, int salary )
                : fn( forename ), sn( surname ), sal( salary )
            { }

            void setSalary( int salary ) { sal = salary; }

            QString forename() const { return fn; }
            QString surname() const { return sn; }
            int salary() const { return sal; }

        private:
            QString fn;
            QString sn;
            int sal;
        };

        QPtrList<Employee> list;
        list.setAutoDelete( TRUE ); // the list owns the objects

        list.append( new Employee("John", "Doe", 50000) );
        list.append( new Employee("Jane", "Williams", 80000) );
        list.append( new Employee("Tom", "Jones", 60000) );

        Employee *employee;
        for ( employee = list.first(); employee; employee = list.next() )
            cout << employee->surname().latin1() << ", " <<
                    employee->forename().latin1() << " earns " <<
                    employee->salary() << endl;
        cout << endl;

        // very inefficient for big lists
        for ( uint i = 0; i < list.count(); ++i )
            if ( list.at(i) )
                cout << list.at( i )->surname().latin1() << endl;

The output is

        Doe, John earns 50000
        Williams, Jane earns 80000
        Jones, Tom earns 60000

        Doe
        Williams
        Jones

QPtrList has several member functions for traversing the list, but using a QPtrListIterator can be more practical. Multiple list iterators may traverse the same list, independently of each other and of the current list item.

In the example above we make the call setAutoDelete(TRUE). Enabling auto-deletion tells the list to delete items that are removed from the list. The default is to not delete items when they are removed but that would cause a memory leak in our example because we have no other references to the list items.

List items are stored as `void*` in an internal QLNode, which also holds pointers to the next and previous list items. The functions currentNode(), removeNode(), and takeNode() operate directly on the QLNode, but they should be used with care. The data component of the node is available through QLNode::getData().

When inserting an item into a list only the pointer is copied, not the item itself, i.e. we make a shallow copy. It is possible to make the list copy all of the item's data (deep copy) when an item is inserted. insert(), inSort() and append() call the virtual function QPtrCollection::newItem() for the item to be inserted. Inherit a list and reimplement it if you want deep copies.

When removing an item from a list, the virtual function QPtrCollection::deleteItem() is called. QPtrList's default implementation is to delete the item if auto-deletion is enabled.

The virtual function compareItems() can be reimplemented to compare two list items. This function is called from all list functions that need to compare list items, for instance remove(const type*). If you only want to deal with pointers, there are functions that compare pointers instead, for instance removeRef(const type*). These functions are somewhat faster than those that call compareItems().

The QStrList class defined in qstrlist.h is a list of `char*`. It reimplements newItem(), deleteItem() and compareItems().

See also QPtrListIterator, Collection Classes and Non-GUI Classes.

## Member Function Documentation

### QPtrList::QPtrList ()

Constructs an empty list.

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

Constructs a copy of list.

Each item in list is appended to this list. Only the pointers are copied (shallow copy).

### QPtrList::~QPtrList ()

Removes all items from the list and destroys the list.

All list iterators that access this list will be reset.

See also setAutoDelete().

### void QPtrList::append ( const type * item )

Inserts the item at the end of the list.

The inserted item becomes the current list item. This is equivalent to `insert( count(), item )`.

The item must not be a null pointer.

See also insert(), current() and prepend().

Examples: customlayout/border.cpp, customlayout/card.cpp, customlayout/flow.cpp, grapher/grapher.cpp, listviews/listviews.cpp, listviews/listviews.h and qwerty/qwerty.cpp.

### type * QPtrList::at ( uint index )

Returns a pointer to the item at position index in the list, or null if the index is out of range.

Sets the current list item to this item if index is valid. The valid range is `0..(count() - 1)` inclusive.

This function is very efficient. It starts scanning from the first item, last item, or current item, whichever is closest to index.

See also current().

Examples: customlayout/border.cpp, customlayout/card.cpp, customlayout/flow.cpp, dirview/dirview.cpp, fileiconview/qfileiconview.cpp, mdi/application.cpp and qwerty/qwerty.cpp.

### int QPtrList::at () const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns the index of the current list item. The returned value is -1 if the current item is null.

See also current().

### bool QPtrCollection::autoDelete () const

Returns the setting of the auto-delete option. The default is FALSE.

See also setAutoDelete().

### void QPtrList::clear ()`[virtual]`

Removes all items from the list.

The removed items are deleted if auto-deletion is enabled.

All list iterators that access this list will be reset.

See also remove(), take() and setAutoDelete().

Reimplemented from QPtrCollection.

### int QPtrList::compareItems ( QPtrCollection::Item item1, QPtrCollection::Item item2 )`[virtual protected]`

This virtual function compares two list items.

Returns:

- zero if item1 == item2
- nonzero if item1 != item2

This function returns int rather than bool so that reimplementations can return three values and use it to sort by:

- 0 if item1 == item2
- > 0 (positive integer) if item1 > item2
- < 0 (negative integer) if item1 < item2

inSort() requires that compareItems() is implemented as described here.

This function should not modify the list because some const functions call compareItems().

The default implementation compares the pointers.

### uint QPtrList::contains ( const type * item ) const

Counts and returns the number of occurrences of item in the list.

The compareItems() function is called when looking for the item in the list. If compareItems() is not reimplemented, it is more efficient to call containsRef().

This function does not affect the current list item.

See also containsRef() and compareItems().

### uint QPtrList::containsRef ( const type * item ) const

Counts and returns the number of occurrences of item in the list.

Calling this function is much faster than contains() because contains() compares item with each list item using compareItems(). This function only compares the pointers.

This function does not affect the current list item.

See also contains().

### uint QPtrList::count () const`[virtual]`

Returns the number of items in the list.

See also isEmpty().

Examples: customlayout/border.cpp, customlayout/card.cpp, customlayout/flow.cpp, fileiconview/qfileiconview.cpp, grapher/grapher.cpp, mdi/application.cpp and qwerty/qwerty.cpp.

Reimplemented from QPtrCollection.

### type * QPtrList::current () const

Returns a pointer to the current list item. The current item may be null (implies that the current index is -1).

See also at().

### QLNode * QPtrList::currentNode () const

Returns a pointer to the current list node.

The node can be kept and removed later using removeNode(). The advantage is that the item can be removed directly without searching the list.

Warning: Do not call this function unless you are an expert.

See also removeNode(), takeNode() and current().

### int QPtrList::find ( const type * item )

Finds the first occurrence of item in the list.

If the item is found, the list sets the current item to point to the found item and returns the index of this item. If the item is not found, the list sets the current item to null, the current index to -1, and returns -1.

The compareItems() function is called when searching for the item in the list. If compareItems() is not reimplemented, it is more efficient to call findRef().

See also findNext(), findRef(), compareItems() and current().

### int QPtrList::findNext ( const type * item )

Finds the next occurrence of item in the list, starting from the current list item.

If the item is found, the list sets the current item to point to the found item and returns the index of this item. If the item is not found, the list sets the current item to null, the current index to -1, and returns -1.

The compareItems() function is called when searching for the item in the list. If compareItems() is not reimplemented, it is more efficient to call findNextRef().

See also find(), findNextRef(), compareItems() and current().

### int QPtrList::findNextRef ( const type * item )

Finds the next occurrence of item in the list, starting from the current list item.

If the item is found, the list sets the current item to point to the found item and returns the index of this item. If the item is not found, the list sets the current item to null, the current index to -1, and returns -1.

Calling this function is much faster than findNext() because findNext() compares item with each list item using compareItems(). This function only compares the pointers.

See also findRef(), findNext() and current().

### int QPtrList::findRef ( const type * item )

Finds the first occurrence of item in the list.

If the item is found, the list sets the current item to point to the found item and returns the index of this item. If the item is not found, the list sets the current item to null, the current index to -1, and returns -1.

Calling this function is much faster than find() because find() compares item with each list item using compareItems(). This function only compares the pointers.

See also findNextRef(), find() and current().

### type * QPtrList::first ()

Returns a pointer to the first item in the list and makes this the current list item, or null if the list is empty.

See also getFirst(), last(), next(), prev() and current().

Examples: grapher/grapher.cpp, listviews/listviews.h and showimg/showimg.cpp.

### type * QPtrList::getFirst () const

Returns a pointer to the first item in the list, or null if the list is empty.

This function does not affect the current list item.

See also first() and getLast().

### type * QPtrList::getLast () const

Returns a pointer to the last item in the list, or null if the list is empty.

This function does not affect the current list item.

See also last() and getFirst().

### void QPtrList::inSort ( const type * item )

Inserts the item at its sorted position in the list.

The sort order depends on the virtual compareItems() function. All items must be inserted with inSort() to maintain the sorting order.

The inserted item becomes the current list item.

The item must not be a null pointer.

Please note that inSort() is slow. If you want to insert lots of items in a list and sort after inserting, you should use sort(). inSort() takes up to O(n) compares. That means inserting n items in your list will need O(n^2) compares whereas sort() only needs O(n*log n) for the same task. So use inSort() only if you already have a presorted list and want to insert just a few additional items.

See also insert(), compareItems(), current() and sort().

### bool QPtrList::insert ( uint index, const type * item )

Inserts the item at the position index in the list.

Returns TRUE if successful or FALSE if index is out of range. The valid range is 0 to count() (inclusively). The item is appended if index == count().

The inserted item becomes the current list item.

The item must not be a null pointer.

See also append() and current().

### bool QPtrList::isEmpty () const

Returns TRUE if the list is empty; otherwise returns FALSE.

See also count().

### type * QPtrList::last ()

Returns a pointer to the last item in the list and makes this the current list item, or null if the list is empty.

See also getLast(), first(), next(), prev() and current().

### type * QPtrList::next ()

Returns a pointer to the item succeeding the current item. Returns null if the current item is null or equal to the last item.

Makes the succeeding item current. If the current item before this function call was the last item, the current item will be set to null. If the current item was null, this function does nothing.

See also first(), last(), prev() and current().

Examples: grapher/grapher.cpp, listviews/listviews.h and showimg/showimg.cpp.

### QPtrList<type> & QPtrList::operator= ( const QPtrList<type> & list )

Assigns list to this list and returns a reference to this list.

This list is first cleared and then each item in list is appended to this list. Only the pointers are copied (shallow copy) unless newItem() has been reimplemented().

### bool QPtrList::operator== ( const QPtrList<type> & list ) const

Compares this list with list. Returns TRUE if the lists contain the same data; otherwise returns FALSE.

### void QPtrList::prepend ( const type * item )

Inserts the item at the start of the list.

The inserted item becomes the current list item. This is equivalent to `insert( 0, item )`.

The item must not be a null pointer.

See also append(), insert() and current().

### type * QPtrList::prev ()

Returns a pointer to the item preceding the current item. Returns null if the current item is null or equal to the first item.

Makes the preceding item current. If the current item before this function call was the first item, the current item will be set to null. If the current item was null, this function does nothing.

See also first(), last(), next() and current().

### QDataStream & QPtrList::read ( QDataStream & s, QPtrCollection::Item & item )`[virtual protected]`

Reads a list item from the stream s and returns a reference to the stream.

The default implementation sets item to 0.

See also write().

### bool QPtrList::remove ( uint index )

Removes the item at position index in the list.

Returns TRUE if successful, or FALSE if index is out of range. The valid range is `0..(count() - 1)` inclusive.

The removed item is deleted if auto-deletion is enabled.

The item after the removed item becomes the new current list item if the removed item is not the last item in the list. If the last item is removed, the new last item becomes the current item.

All list iterators that refer to the removed item will be set to point to the new current item.

See also take(), clear(), setAutoDelete(), current() and removeRef().

### bool QPtrList::remove ()

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Removes the current list item.

Returns TRUE if successful, or FALSE if the current item is null.

The removed item is deleted if auto-deletion is enabled.

The item after the removed item becomes the new current list item if the removed item is not the last item in the list. If the last item is removed, the new last item becomes the current item. The current item is set to null if the list becomes empty.

All list iterators that refer to the removed item will be set to point to the new current item.

See also take(), clear(), setAutoDelete(), current() and removeRef().

### bool QPtrList::remove ( const type * item )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Removes the first occurrence of item from the list.

Returns TRUE if successful, or FALSE if the item could not be found in the list.

The removed item is deleted if auto-deletion is enabled.

The compareItems() function is called when searching for the item in the list. If compareItems() is not reimplemented, it is more efficient to call removeRef().

The item after the removed item becomes the new current list item if the removed item is not the last item in the list. If the last item is removed, the new last item becomes the current item. The current item is set to null if the list becomes empty.

All list iterators that refer to the removed item will be set to point to the new current item.

See also removeRef(), take(), clear(), setAutoDelete(), compareItems() and current().

### bool QPtrList::removeFirst ()

Removes the first item from the list. Returns TRUE if successful, or FALSE if the list is empty.

The removed item is deleted if auto-deletion is enabled.

The first item in the list becomes the new current list item. The current item is set to null if the list becomes empty.

All list iterators that refer to the removed item will be set to point to the new current item.

See also removeLast(), setAutoDelete(), current() and remove().

### bool QPtrList::removeLast ()

Removes the last item from the list. Returns TRUE if successful, or FALSE if the list is empty.

The removed item is deleted if auto-deletion is enabled.

The last item in the list becomes the new current list item. The current item is set to null if the list becomes empty.

All list iterators that refer to the removed item will be set to point to the new current item.

See also removeFirst(), setAutoDelete() and current().

### void QPtrList::removeNode ( QLNode * node )

Removes the node from the list.

This node must exist in the list, otherwise the program may crash.

The removed item is deleted if auto-deletion is enabled.

The first item in the list will become the new current list item. The current item is set to null if the list becomes empty.

All list iterators that refer to the removed item will be set to point to the item succeeding this item or to the preceding item if the removed item was the last item.

Warning: Do not call this function unless you are an expert.

See also takeNode(), currentNode(), remove() and removeRef().

### bool QPtrList::removeRef ( const type * item )

Removes the first occurrence of item from the list.

Returns TRUE if successful, or FALSE if the item cannot be found in the list.

The removed item is deleted if auto-deletion is enabled.

The list is scanned until the pointer item is found. It is removed if it is found.

Equivalent to:

        if ( list.findRef( item ) != -1 )
            list.remove();

The item after the removed item becomes the new current list item if the removed item is not the last item in the list. If the last item is removed, the new last item becomes the current item. The current item is set to null if the list becomes empty.

All list iterators that refer to the removed item will be set to point to the new current item.

See also remove(), clear(), setAutoDelete() and current().

### void QPtrCollection::setAutoDelete ( bool enable )

Sets the collection to auto-delete its contents if enable is TRUE and to never delete them if enable is FALSE.

If auto-deleting is turned on, all the items in a collection are deleted when the collection itself is deleted. This is convenient if the collection has the only pointer to the items.

The default setting is FALSE, for safety. If you turn it on, be careful about copying the collection - you might find yourself with two collections deleting the same items.

Note that the auto-delete setting may also affect other functions in subclasses. For example, a subclass that has a remove() function will remove the item from its data structure, and if auto-delete is enabled, will also delete the item.

See also autoDelete().

Examples: grapher/grapher.cpp, scribble/scribble.cpp and table/bigtable/main.cpp.

### void QPtrList::sort ()

Sorts the list by the result of the virtual compareItems() function.

The Heap-Sort algorithm is used for sorting. It sorts n items with O(n*log n) comparisons. This is the asymptotic optimal solution of the sorting problem.

If the items in your list support operator< and operator==, you might be better off with QSortedList because it implements the compareItems() function for you using these two operators.

See also inSort().

### type * QPtrList::take ( uint index )

Takes the item at position index out of the list without deleting it (even if auto-deletion is enabled).

Returns a pointer to the item taken out of the list, or null if the index is out of range. The valid range is `0..(count() - 1)` inclusive.

The item after the removed item becomes the new current list item if the removed item is not the last item in the list. If the last item is removed, the new last item becomes the current item. The current item is set to null if the list becomes empty.

All list iterators that refer to the taken item will be set to point to the new current item.

See also remove(), clear() and current().

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

### type * QPtrList::take ()

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Takes the current item out of the list without deleting it (even if auto-deletion is enabled). Returns a pointer to the item taken out of the list, or null if the current item is null.

The item after the removed item becomes the new current list item if the removed item is not the last item in the list. If the last item is removed, the new last item becomes the current item. The current item is set to null if the list becomes empty.

All list iterators that refer to the taken item will be set to point to the new current item.

See also remove(), clear() and current().

### type * QPtrList::takeNode ( QLNode * node )

Takes the node out of the list without deleting its item (even if auto-deletion is enabled). Returns a pointer to the item taken out of the list.

This node must exist in the list, otherwise the program may crash.

The first item in the list becomes the new current list item.

All list iterators that refer to the taken item will be set to point to the item succeeding this item or to the preceding item if the taken item was the last item.

Warning: Do not call this function unless you are an expert.

See also removeNode() and currentNode().

### void QPtrList::toVector ( QGVector * vec ) const

Stores all list items in the vector vec.

The vector must be have the same item type, otherwise the result will be undefined.

### QDataStream & QPtrList::write ( QDataStream & s, QPtrCollection::Item item ) const`[virtual protected]`

Writes a list item, item to the stream s and returns a reference to the stream.

The default implementation does nothing.

See also read().

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