QList(3qt)

NAME
QList - Template class that provides doubly linked lists

#include <qlist.h>

Inherits QGList.

Inherited by QStrList.

  Public Members
QList ()
QList ( const QList<type> & list )
~QList ()
QList<type>& operator= ( const QList<type> & list )
bool operator== ( const QList<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

DESCRIPTION
The QList class is a template class that provides doubly linked lists.

In Qt 2.0 QList is only implemented as a template class. Define a template instance QList<X> to create a list that operates on pointers to X, or X*.

Example:

    #include <qlist.h>
    #include <qstring.h>
    #include <stdio.h>
    class Employee
    {
    public:
        Employee( const QString& name, int salary ) { n=name; s=salary; }
        QString     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) );
        Employee *emp;
        for ( emp=list.first(); emp != 0; emp=list.next() )
            printf( "%s earns %d\n", emp->name().latin1(), emp->salary() );
    }

Program output:

        Bill earns 50000
        Steve earns 80000
        Ron earns 60000

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.

QList has several member functions for traversing the list, but using a QListIterator can be more practical. Multiple list iterators may traverse the same list, independent of each other and independent 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 since 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.

When inserting an item into a list, only the pointer is copied, not the item itself. This is called a shallow copy. It is possible to make the list copy all of the item's data (known as a deep copy) when an item is inserted. insert(), inSort() and append() call the virtual function QCollection::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 QCollection::deleteItem() is called. QList's default implementation is to delete the item if auto-deletion is enabled.

The virtual function QGList::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 in qstrlist.h is a list of char*. QStrList is a good example of a list that reimplements newItem(), deleteItem() and compareItems()

See also QListIterator and Collection Classes

Examples: grapher/grapher.cpp

MEMBER FUNCTION DOCUMENTATION

QList::QList ()
Constructs an empty list.

QList::QList ( const QList<type> & list )
Constructs a copy of list.

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

QList::~QList ()
Removes all items from the list and destroys the list.

All list iterators that access this list will be reset.

See also setAutoDelete().

void QList::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: grapher/grapher.cpp

int QList::at () const
Returns the index of the current list item. The returned value is -1 if the current item is null.

See also current().

type * QList::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().

void QList::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 QCollection.

uint QList::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().

Does not affect the current list item.

See also containsRef() and compareItems().

uint QList::containsRef ( const type * item ) const
Counts and returns the number of occurrences of item in the list.

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

Does not affect the current list item.

See also contains().

uint QList::count () const [virtual]
Returns the number of items in the list.

See also isEmpty().

Reimplemented from QCollection.

type * QList::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 * QList::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 QList::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 QList::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 QList::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 must 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 QList::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 must 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 * QList::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

type * QList::getFirst () const
Returns a pointer to the first item in the list, or null if the list is empty.

Does not affect the current list item.

See also first() and getLast().

type * QList::getLast () const
Returns a pointer to the last item in the list, or null if the list is empty.

Does not affect the current list item.

See also last() and getFirst().

void QList::inSort ( const type * item )
Inserts the item at its sorted position in the list.

The sort order depends on the virtual QGList::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 then 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 while sort() only needs O(n*logn) for the same task. So you inSort() only if you already have a pre-sorted list and want to insert only few additional items.

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

bool QList::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 .. count() inclusive. 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 QList::isEmpty () const
Returns TRUE if the list is empty, i.e. count() == 0. Returns FALSE otherwise.

See also count().

type * QList::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 * QList::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

QList<type> & QList::operator= ( const QList<type> & list )
Assigns list to this list and returns a reference to this list.

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

bool QList::operator== ( const QList<type> & list ) const
Compares this list with list. Retruns TRUE if the lists contain the same data, else FALSE.

void QList::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 * QList::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().

bool QList::remove ()
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 QList::remove ( const type * item )
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 QList::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. 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 QList::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 QList::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 QList::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 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 QList::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 QList::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) compares. This is the asymptotic optimal solution of the sorting problem.

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

See also inSort().

type * QList::take ()
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 taken item becomes the new current list item if the taken item is not the last item in the list. If the last item is taken, 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 * QList::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 taken item becomes the new current list item if the taken item is not the last item in the list. If the last item is taken, 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 * QList::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 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 QList::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.

SEE ALSO
http://doc.trolltech.com/qlist.html http://www.trolltech.com/faq/tech.html

COPYRIGHT
Copyright 1992-2001 Trolltech AS, http://www.trolltech.com. See the license file included in the distribution for a complete license statement.

AUTHOR
Generated automatically from the source code.

BUGS
If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports make our job much simpler. Thank you.

In case of content or formattting problems with this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qlist.3) and the Qt version (2.3.2).