Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |
The QList class is a template class that provides lists. More...
#include <QList>
Inherited by QItemSelection, QQueue, QSignalSpy, QStringList, and QTestEventList.
Note: All the functions in this class are reentrant.
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:
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, and QVector.
Qt-style synonym for QList::const_iterator.
Qt-style synonym for QList::iterator.
Typedef for const T *. Provided for STL compatibility.
Typedef for const T &. Provided for STL compatibility.
Typedef for T *. Provided for STL compatibility.
Typedef for T &. Provided for STL compatibility.
Typedef for int. Provided for STL compatibility.
Typedef for T. Provided for STL compatibility.
Constructs an empty list.
Constructs a copy of other.
This operation takes constant time, because QList is implicitly shared. This makes returning a QList from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and that takes linear time.
See also operator=().
Destroys the list. References to the values in the list and all iterators of this list become invalid.
Inserts value at the end of the list.
Example:
QList<QString> list; list.append("one"); list.append("two"); list.append("three"); // list: ["one", "two", "three"]
This is the same as list.insert(size(), value).
This operation is typically very fast (constant time), because QList preallocates extra space on both sides of its internal buffer to allow for fast growth at both ends of the list.
See also operator<<(), prepend(), and insert().
Returns the item at index position i in the list.
i must be a valid index position in the list (i.e., 0 <= i < size()).
This function is very fast (constant time).
See also value() and operator[]().
This function is provided for STL compatibility. It is equivalent to last().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Returns an STL-style iterator pointing to the first item in the list.
See also constBegin() and end().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Removes all items from the list.
See also removeAll().
Returns a const STL-style iterator pointing to the first item in the list.
See also begin() and constEnd().
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list.
See also constBegin() and end().
Returns true if the list contains an occurrence of value; otherwise returns false.
This function requires the value type to have an implementation of operator==().
See also indexOf() and count().
Returns the number of occurrences of value in the list.
This function requires the value type to have an implementation of operator==().
See also contains() and indexOf().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Returns the number of items in the list. This is effectively the same as size().
This function is provided for STL compatibility. It is equivalent to isEmpty().
Returns an STL-style iterator pointing to the imaginary item after the last item in the list.
See also begin() and constEnd().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Removes the item associated with the iterator pos from the list, and returns an iterator to the next item in the list (which may be end()).
See also insert() and removeAt().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Removes all the items from begin up to (but not including) end. Returns an iterator to the same item that end referred to before the call.
Returns a reference to the first item in the list. This function assumes that the list isn't empty.
See also last() and isEmpty().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Returns a QList object with the data contained in set. The order of the elements in the QList is undefined.
Example:
QSet<double> set; set << "red" << "green" << "blue" << ... << "black"; QList<double> list = QList<double>::fromSet(set); qSort(list);
See also fromVector(), toSet(), QSet::toList(), and qSort().
Returns a QList object with the data contained in list. The order of the elements in the QList is the same as in list.
Example:
std::list<double> stdlist; list.push_back(1.2); list.push_back(0.5); list.push_back(3.14); QList<double> list = QList<double>::fromStdList(stdlist);
See also toStdList() and QVector::fromStdVector().
Returns a QList object with the data contained in vector.
Example:
QVector<double> vect; vect << "red" << "green" << "blue" << "black"; QList<double> list = QVector<T>::fromVector(vect); // list: ["red", "green", "blue", "black"]
See also fromSet(), toVector(), and QVector::toList().
This function is provided for STL compatibility. It is equivalent to first().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Returns the index position of the first occurrence of value in the list, searching forward from index position from. Returns -1 if no item matched.
Example:
QList<QString> list; list << "A" << "B" << "C" << "B" << "A"; list.indexOf("B"); // returns 1 list.indexOf("B", 1); // returns 1 list.indexOf("B", 2); // returns 3 list.indexOf("X"); // returns -1
This function requires the value type to have an implementation of operator==().
See also lastIndexOf() and contains().
Inserts value at index position i in the list. If i is 0, the value is prepended to the list. If i is size(), the value is appended to the list.
Example:
QList<QString> list; list << "alpha" << "beta" << "delta"; list.insert(2, "gamma"); // list: ["alpha", "beta", "gamma", "delta"]
See also append(), prepend(), replace(), and removeAt().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Inserts value in front of the item pointed to by the iterator before. Returns an iterator pointing at the inserted item.
Returns true if the list contains no items; otherwise returns false.
See also size().
Returns a reference to the last item in the list. This function assumes that the list isn't empty.
See also first() and isEmpty().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Returns the index position of the last occurrence of value in the list, searching backward from index position from. If from is -1 (the default), the search starts at the last item. Returns -1 if no item matched.
Example:
QList<QString> list; list << "A" << "B" << "C" << "B" << "A"; list.lastIndexOf("B"); // returns 3 list.lastIndexOf("B", 3); // returns 3 list.lastIndexOf("B", 2); // returns 1 list.lastIndexOf("X"); // returns -1
This function requires the value type to have an implementation of operator==().
See also indexOf().
Returns a list whose elements are copied from this list, starting at position pos. If length is -1 (the default), all elements after pos are copied; otherwise length elements (or all remaining elements if there are less than length elements) are copied.
Moves the item at index position from to index position to.
Example:
QList<QString> list; list << "A" << "B" << "C" << "D" << "E" << "F"; list.move(1, 4); // list: ["A", "C", "D", "E", "B", "F"]
This is the same as insert(to, takeAt(from)).
See also swap(), insert(), and takeAt().
This function is provided for STL compatibility. It is equivalent to removeLast().
This function is provided for STL compatibility. It is equivalent to removeFirst().
Inserts value at the beginning of the list.
Example:
QList<QString> list; list.prepend("one"); list.prepend("two"); list.prepend("three"); // list: ["three", "two", "one"]
This is the same as list.insert(0, value).
This operation is usually very fast (constant time), because QList preallocates extra space on both sides of its internal buffer to allow for fast growth at both ends of the list.
See also append() and insert().
This function is provided for STL compatibility. It is equivalent to append(value).
This function is provided for STL compatibility. It is equivalent to prepend(value).
Removes all occurrences of value in the list and returns the number of entries removed.
Example:
QList<QString> list; list << "sun" << "cloud" << "sun" << "rain"; list.removeAll("sun"); // list: ["cloud", "rain"]
This function requires the value type to have an implementation of operator==().
See also removeAt(), takeAt(), and replace().
Removes the item at index position i.
i must be a valid index position in the list (i.e., 0 <= i < size()).
See also takeAt(), removeFirst(), and removeLast().
Removes the first item in the list.
This is the same as removeAt(0).
See also removeAt() and takeFirst().
Removes the last item in the list.
This is the same as removeAt(size() - 1).
See also removeAt() and takeLast().
Replaces the item at index position i with value.
i must be a valid index position in the list (i.e., 0 <= i < size()).
See also operator[]() and removeAt().
Returns the number of items in the list.
See also isEmpty() and count().
Exchange the item at index position i with the item at index position j.
Example:
QList<QString> list; list << "A" << "B" << "C" << "D" << "E" << "F"; list.swap(1, 4); // list: ["A", "E", "C", "D", "B", "F"]
See also move().
Removes the item at index position i and returns it.
i must be a valid index position in the list (i.e., 0 <= i < size()).
If you don't use the return value, removeAt() is more efficient.
See also removeAt(), takeFirst(), and takeLast().
Removes the first item in the list and returns it.
This is the same as takeAt(0).
This operation is very fast (constant time), 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 don't use the return value, removeFirst() is more efficient.
See also takeLast(), takeAt(), and removeFirst().
Removes the last item in the list and returns it.
This is the same as takeAt(size() - 1).
This operation is very fast (constant time), 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 don't use the return value, removeLast() is more efficient.
See also takeFirst(), takeAt(), and removeLast().
Returns a QSet object with the data contained in this QList. Since QSet doesn't allow duplicates, the resulting QSet might be smaller than the original list was.
Example:
QStringList list; list << "Julia" << "Mike" << "Mike" << "Julia" << "Julia"; QSet<QString> set = list.toSet(); set.contains("Julia"); // returns true set.contains("Mike"); // returns true set.size(); // returns 2
See also toVector(), fromSet(), and QSet::fromList().
Returns a std::list object with the data contained in this QList. Example:
QList<double> list; list << 1.2 << 0.5 << 3.14; std::list<double> stdlist = list.toStdList();
See also fromStdList() and QVector::toStdVector().
Returns a QVector object with the data contained in this QList.
Example:
QStringList list; list << "Sven" << "Kim" << "Ola"; QVector<QString> vect = list.toVector(); // vect: ["Sven", "Kim", "Ola"]
See also toSet(), fromVector(), and QVector::fromList().
Returns the value at index position i in the list.
If the index i is out of bounds, the function returns a default-constructed value. If you are certain that the index is going to be within bounds, you can use at() instead, which is slightly faster.
See also at() and operator[]().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
If the index i is out of bounds, the function returns defaultValue.
Returns true if other is not equal to this list; otherwise returns false.
Two lists are considered equal if they contain the same values in the same order.
This function requires the value type to have an implementation of operator==().
See also operator==().
Returns a list that contains all the items in this list followed by all the items in the other list.
See also operator+=().
Appends the items of the other list to this list and returns a reference to this list.
See also operator+() and append().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Appends value to the list.
See also append() and operator<<().
Appends the items of the other list to this list and returns a reference to this list.
See also operator+=() and append().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Appends value to the list.
Assigns other to this list and returns a reference to this list.
Returns true if other is equal to this list; otherwise returns false.
Two lists are considered equal if they contain the same values in the same order.
This function requires the value type to have an implementation of operator==().
See also operator!=().
Returns the item at index position i as a modifiable reference.
i must be a valid index position in the list (i.e., 0 <= i < size()).
This function is very fast (constant time).
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Same as at().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Writes the list list to stream out.
This function requires the value type to implement operator<<().
See also Format of the QDataStream operators.
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Reads a list from stream in into list.
This function requires the value type to implement operator>>().
See also Format of the QDataStream operators.
Copyright © 2005 Trolltech | Trademarks | Qt 4.1.0 |