Logo Search packages:      
Sourcecode: qt4-x11 version File versions

QList< T > Class Template Reference

Inheritance diagram for QList< T >:

QPatternist::FunctionFactoryCollection QQueue< T >

List of all members.

Detailed Description

template<typename T>
class QList< T >

The QList class is a template class that provides lists.

QList<T> is one of Qt's generic {container classes}. It stores a list of values and provides fast index-based access as well as fast insertions and removals.

QList<T>, QLinkedList<T>, and QVector<T> provide similar functionality. Here's an overview:

For most purposes, QList is the right class to use. Its index-based API is more convenient than QLinkedList's iterator-based API, and it is usually faster than QVector because of the way it stores its items in memory. It also expands to less code in your executable. If you need a real linked list, with guarantees of {constant time} insertions in the middle of the list and iterators to items rather than indexes, use QLinkedList. If you want the items to occupy adjacent memory positions, use QVector.

Internally, QList<T> is represented as an array of pointers to items of type T. If T is itself a pointer type or a basic type that is no larger than a pointer, or if T is one of Qt's {shared classes}, then QList<T> stores the items directly in the pointer array. For lists under a thousand items, this array representation allows for very fast insertions in the middle, and it allows index-based access. Furthermore, operations like prepend() and append() are very fast, because QList preallocates memory at both ends of its internal array. (See {Algorithmic Complexity} for details.) Note, however, that for unshared list items that are larger than a pointer, each append or insert of a new item requires allocating the new item on the heap, and this per item allocation might make QVector a better choice in cases that do lots of appending or inserting, since QVector allocates memory for its items in a single heap allocation.

Note that the internal array only ever gets bigger over the life of the list. It never shrinks. The internal array is deallocated by the destructor and by the assignment operator, when one list is assigned to another.

Here's an example of a QList that stores integers and a QList that stores QDate values:

doc/src/snippets/code/src_corelib_tools_qlistdata.cpp 0

Qt includes a QStringList class that inherits QList<QString> and adds a few convenience functions, such as QStringList::join() and QStringList::find(). (QString::split() creates QStringLists from strings.)

QList stores a list of items. The default constructor creates an empty list. To insert items into the list, you can use operator<<():

doc/src/snippets/code/src_corelib_tools_qlistdata.cpp 1

QList provides these basic functions to add, move, and remove items: insert(), replace(), removeAt(), move(), and swap(). In addition, it provides the following convenience functions: append(), prepend(), removeFirst(), and removeLast().

QList uses 0-based indexes, just like C++ arrays. To access the item at a particular index position, you can use operator[](). On non-const lists, operator[]() returns a reference to the item and can be used on the left side of an assignment:

doc/src/snippets/code/src_corelib_tools_qlistdata.cpp 2

Because QList is implemented as an array of pointers, this operation is very fast ({constant time}). For read-only access, an alternative syntax is to use at():

doc/src/snippets/code/src_corelib_tools_qlistdata.cpp 3

at() can be faster than operator[](), because it never causes a {deep copy} to occur.

A common requirement is to remove an item from a list and do something with it. For this, QList provides takeAt(), takeFirst(), and takeLast(). Here's a loop that removes the items from a list one at a time and calls delete on them:

doc/src/snippets/code/src_corelib_tools_qlistdata.cpp 4

Inserting and removing items at either ends of the list is very fast ({constant time} in most cases), because QList preallocates extra space on both sides of its internal buffer to allow for fast growth at both ends of the list.

If you want to find all occurrences of a particular value in a list, use indexOf() or lastIndexOf(). The former searches forward starting from a given index position, the latter searches backward. Both return the index of a matching item if they find it; otherwise, they return -1. For example:

doc/src/snippets/code/src_corelib_tools_qlistdata.cpp 5

If you simply want to check whether a list contains a particular value, use contains(). If you want to find out how many times a particular value occurs in the list, use count(). If you want to replace all occurrences of a particular value with another, use replace().

QList's value type must be an {assignable data type}. This covers most data types that are commonly used, but the compiler won't let you, for example, store a QWidget as a value; instead, store a QWidget *. A few functions have additional requirements; for example, indexOf() and lastIndexOf() expect the value type to support operator==(). These requirements are documented on a per-function basis.

Like the other container classes, QList provides {Java-style iterators} (QListIterator and QMutableListIterator) and {STL-style iterators} (QList::const_iterator and QList::iterator). In practice, these are rarely used, because you can use indexes into the QList. QList is implemented in such a way that direct index-based access is just as fast as using iterators.

QList does not support inserting, prepending, appending or replacing with references to its own values. Doing so will cause your application to abort with an error message.

To make QList as efficient as possible, its member functions don't validate their input before using it. Except for isEmpty(), member functions always assume the list is not empty. Member functions that take index values as parameters always assume their index value parameters are in the valid range. This means QList member functions can fail. If you define QT_NO_DEBUG when you compile, failures will not be detected. If you don't define QT_NO_DEBUG, failures will be detected using Q_ASSERT() or Q_ASSERT_X() with an appropriate message.

To avoid failures when your list can be empty, call isEmpty() before calling other member functions. If you must pass an index value that might not be in the valid range, check that it is less than the value returned by size() but not less than 0.

See also:
QListIterator, QMutableListIterator, QLinkedList, QVector

Definition at line 90 of file qlist.h.

Public Types

typedef const value_typeconst_pointer
typedef const value_typeconst_reference
typedef const_iterator ConstIterator
typedef ptrdiff_t difference_type
typedef iterator Iterator
typedef value_typepointer
typedef value_typereference
typedef int size_type
typedef T value_type

Public Member Functions

void append (const QList< T > &t)
void append (const T &t)
const T & at (int i) const
const T & back () const
T & back ()
const_iterator begin () const
iterator begin ()
void clear ()
const_iterator constBegin () const
const_iterator constEnd () const
QBool contains (const T &t) const
int count () const
int count (const T &t) const
void detach ()
bool empty () const
const_iterator end () const
iterator end ()
bool endsWith (const T &t) const
iterator erase (iterator first, iterator last)
iterator erase (iterator pos)
const T & first () const
T & first ()
const T & front () const
T & front ()
int indexOf (const T &t, int from=0) const
iterator insert (iterator before, const T &t)
void insert (int i, const T &t)
bool isDetached () const
bool isEmpty () const
const T & last () const
T & last ()
int lastIndexOf (const T &t, int from=-1) const
int length () const
QList< T > mid (int pos, int length=-1) const
void move (int from, int to)
bool operator!= (const QList< T > &l) const
QList< T > operator+ (const QList< T > &l) const
QList< T > & operator+= (const T &t)
QList< T > & operator+= (const QList< T > &l)
QList< T > & operator<< (const QList< T > &l)
QList< T > & operator<< (const T &t)
QList< T > & operator= (const QList< T > &l)
bool operator== (const QList< T > &l) const
T & operator[] (int i)
const T & operator[] (int i) const
void pop_back ()
void pop_front ()
void prepend (const T &t)
void push_back (const T &t)
void push_front (const T &t)
 QList (const QList< T > &l)
 QList ()
int removeAll (const T &t)
void removeAt (int i)
void removeFirst ()
void removeLast ()
bool removeOne (const T &t)
void replace (int i, const T &t)
void setSharable (bool sharable)
int size () const
bool startsWith (const T &t) const
void swap (int i, int j)
takeAt (int i)
takeFirst ()
takeLast ()
QSet< T > toSet () const
std::list< T > toStdList () const
QVector< T > toVector () const
value (int i, const T &defaultValue) const
value (int i) const
 ~QList ()

Static Public Member Functions

static QList< T > fromSet (const QSet< T > &set)
static QList< T > fromStdList (const std::list< T > &list)
static QList< T > fromVector (const QVector< T > &vector)

Private Member Functions

void detach_helper ()
void free (QListData::Data *d)
void node_construct (Node *n, const T &t)
void node_copy (Node *from, Node *to, Node *src)
void node_destruct (Node *from, Node *to)
void node_destruct (Node *n)

Private Attributes

union {
   QListData::Data *   d
   QListData   p


class const_iterator
class iterator

Related Functions

(Note that these are not member functions.)

QDataStreamoperator<< (QDataStream &out, const QList< T > &list)
QDataStreamoperator>> (QDataStream &in, QList< T > &list)


class  const_iterator
 The QList::const_iterator class provides an STL-style const iterator for QList and QQueue. More...
class  iterator
 The QList::iterator class provides an STL-style non-const iterator for QList and QQueue. More...
struct  Node

The documentation for this class was generated from the following files:

Generated by  Doxygen 1.6.0   Back to index