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

QList< T > Class Template Reference

Inheritance diagram for QList< T >:

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. (Exceptionally, if T is a pointer type, a basic type of the size of a pointer, or one of Qt's {shared classes}, QList<T> stores the item directly in the pointer.) For lists under a thousand items, this representation allows for very fast insertions in the middle, in addition to instantaneous index-based access. Furthermore, operations like prepend() and append() are very fast, because QList preallocates memory on both sides of its internal array.

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

        QList<int> integerList;
        QList<QDate> dateList;

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<<():

        QList<QString> list;
        list << "one" << "two" << "three";
        // list: ["one", "two", "three"]

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:

        if (list[0] == "Bob")
            list[0] = "Robert";

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():

        for (int i = 0; i < list.size(); ++i) {
            if (list.at(i) == "Jane")
                cout << "Found Jane at position " << i << endl;

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:

        QList<QWidget *> list;
        while (!list.isEmpty())
            delete list.takeFirst();

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:

        int i = list.indexOf("Jane");
        if (i != -1)
            cout << "First occurrence of Jane is at position " << i << endl;

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.

See also:
QListIterator, QMutableListIterator, QLinkedList, QVector

Definition at line 74 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 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 ()
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
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 ()
void replace (int i, const T &t)
void setSharable (bool sharable)
int size () 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