QList Class▲
-
Header: QList
-
qmake: QT += core
-
Inherited By: https://
-
Group: QList is part of tools, https://
Detailed Description▲
https://<T> is one of Qt's generic https://. It stores items in a list that provides fast index-based access and index-based insertions and removals.
https://<T>, https://<T>, and https://<T> provide similar APIs and functionality. They are often interchangeable, but there are performance consequences. Here is an overview of use cases:
-
https:// should be your default first choice. https://<T> will usually give better performance than https://<T>, because https://<T> always stores its items sequentially in memory, where https://<T> will allocate its items on the heap unless sizeof(T) <= sizeof(void*) and T has been declared to be either a Q_MOVABLE_TYPE or a Q_PRIMITIVE_TYPE using Q_DECLARE_TYPEINFO. See the http:// for an explanation.
-
However, https:// is used throughout the Qt APIs for passing parameters and for returning values. Use https:// to interface with those APIs.
-
If you need a real linked list, which guarantees https:// insertions mid-list and uses iterators to items rather than indexes, use https://.
https:// and https:// both guarantee C-compatible array layout. https:// does not. This might be important if your application must interface with a C API.
Iterators into a https:// and references into heap-allocating QLists remain valid as long as the referenced items remain in the container. This is not true for iterators and references into a https:// and non-heap-allocating QLists.
Internally, https://<T> is represented as an array of T if sizeof(T) <= sizeof(void*) and T has been declared to be either a Q_MOVABLE_TYPE or a Q_PRIMITIVE_TYPE using Q_DECLARE_TYPEINFO. Otherwise, https://<T> is represented as an array of T* and the items are allocated on the heap.
The array representation allows very fast insertions and index-based access. The https://() and https://() operations are also very fast because https:// preallocates memory at both ends of its internal array. (See https:// for details.
Note, however, that when the conditions specified above are not met, each append or insert of a new item requires allocating the new item on the heap, and this per item allocation will make https:// a better choice for use cases that do a lot of appending or inserting, because https:// can allocate memory for many 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 https:// that stores integers and a https:// that stores QDate values:
QList<int> integerList;
QList<QDate> dateList;Qt includes a https:// class that inherits https://<QString> and adds a few convenience functions, such as https://() and https://(). QString::split() creates QStringLists from strings.
https:// stores a list of items. The default constructor creates an empty list. You can use the initializer-list constructor to create a list with elements:
QList<QString> list = { "one", "two", "three" };https:// provides these basic functions to add, move, and remove items: https://(), https://(), https://(), https://(), and swap(). In addition, it provides the following convenience functions: https://(), https://(), https://(), https://(), https://(), and https://().
https://() allows to conveniently add multiple elements to a list:
list << "four" << "five";https:// 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 https:// is implemented as an array of pointers for types that are larger than a pointer or are not movable, this operation requires (https://). For read-only access, an alternative syntax is to use https://():
for (int i = 0; i < list.size(); ++i) {
if (list.at(i) == "Jane")
cout << "Found Jane at position " << i << endl;
}https://() can be faster than operator[](), because it never causes a https:// to occur.
A common requirement is to remove an item from a list and do something with it. For this, https:// provides https://(), https://(), and https://(). 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 end of the list is very fast (https:// in most cases), because https:// 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 https://(). 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 https://(). If you want to find out how many times a particular value occurs in the list, use https://(). If you want to replace all occurrences of a particular value with another, use https://().
https://'s value type must be an https://. 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 https://() expect the value type to support operator==(). These requirements are documented on a per-function basis.
Like the other container classes, https:// provides https:// (QListIterator and QMutableListIterator) and https:// (https:// and https://). In practice, these are rarely used, because you can use indexes into the https://. https:// is implemented in such a way that direct index-based access is just as fast as using iterators.
https:// 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 https:// as efficient as possible, its member functions don't validate their input before using it. Except for https://, 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 https:// 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 https://() or https://() with an appropriate message.
To avoid failures when your list can be empty, call https:// 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 https://() but not less than 0.
More Members▲
If T is a QByteArray, this class has a couple more members that can be used. See the documentation for https:// for more information.
If T is QString, this class has the following additional members: https://, https://, https://, https://.
More Information on Using Qt Containers▲
For a detailed discussion comparing Qt containers with each other and with STL containers, see http://.
See Also▲
See also QListIterator, QMutableListIterator, https://, https://
Member Type Documentation▲
QList::ConstIterator▲
Qt-style synonym for https://.
QList::Iterator▲
Qt-style synonym for https://.
QList::const_pointer▲
Typedef for const T *. Provided for STL compatibility.
QList::const_reference▲
Typedef for const T &. Provided for STL compatibility.
[since 5.6] QList::const_reverse_iterator▲
The QList::const_reverse_iterator typedef provides an STL-style const reverse iterator for https://.
It is simply a typedef for std::reverse_iterator<const_iterator>.
Iterators on implicitly shared containers do not work exactly like STL-iterators. You should avoid copying a container while iterators are active on that container. For more information, read https://.
This typedef was introduced in Qt 5.6.
See Also▲
See also QList::rbegin(), QList::rend(), https://, https://
QList::difference_type▲
Typedef for ptrdiff_t. Provided for STL compatibility.
QList::pointer▲
Typedef for T *. Provided for STL compatibility.
QList::reference▲
Typedef for T &. Provided for STL compatibility.
[since 5.6] QList::reverse_iterator▲
The QList::reverse_iterator typedef provides an STL-style non-const reverse iterator for https://.
It is simply a typedef for std::reverse_iterator<iterator>.
Iterators on implicitly shared containers do not work exactly like STL-iterators. You should avoid copying a container while iterators are active on that container. For more information, read https://.
This typedef was introduced in Qt 5.6.
See Also▲
See also QList::rbegin(), QList::rend(), https://, https://
QList::size_type▲
Typedef for int. Provided for STL compatibility.
QList::value_type▲
Typedef for T. Provided for STL compatibility.
Member Function Documentation▲
QList::QList()▲
Constructs an empty list.
[since 5.2] QList::QList(QList<T> &&other)▲
Move-constructs a https:// instance, making it point at the same object that other was pointing to.
This function was introduced in Qt 5.2.
[default] QList::QList(const QList<T> &other)▲
Constructs a copy of other.
This operation takes https://, because https:// is https://. This makes returning a https:// from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and that takes https://.
See Also▲
See also https://()
[default, since 4.8] QList::QList(std::initializer_list<T> args)▲
Construct a list from the std::initializer_list specified by args.
This constructor is only enabled if the compiler supports C++11 initializer lists.
This function was introduced in Qt 4.8.
QList::~QList()▲
Destroys the list. References to the values in the list and all iterators of this list become invalid.
void QList::append(const T &value)▲
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(https://(), value).
If this list is not shared, this operation is typically very fast (amortized https://), because https:// preallocates extra space on both sides of its internal buffer to allow for fast growth at both ends of the list.
See Also▲
See also https://(), https://(), https://()
[since 4.5] void QList::append(const QList<T> &value)▲
This is an overloaded function.
Appends the items of the value list to this list.
This function was introduced in Qt 4.5.
See Also▲
See also https://(), https://()
const T &QList::at(int i) const▲
Returns the item at index position i in the list. i must be a valid index position in the list (i.e., 0 <= i < https://()).
This function is very fast (https://).
See Also▲
See also https://(), https://()
T &QList::back()▲
This function is provided for STL compatibility. It is equivalent to https://(). The list must not be empty. If the list can be empty, call https:// before calling this function.
const T &QList::back() const▲
This is an overloaded function.
QList::iterator QList::begin()▲
QList::const_iterator QList::begin() const▲
This is an overloaded function.
[since 5.6] const T &QList::constFirst() const▲
Returns a const reference to the first item in the list. The list must not be empty. If the list can be empty, call https:// before calling this function.
This function was introduced in Qt 5.6.
See Also▲
See also https://(), https://, https://()
[since 5.6] const T &QList::constLast() const▲
Returns a reference to the last item in the list. The list must not be empty. If the list can be empty, call https:// before calling this function.
This function was introduced in Qt 5.6.
See Also▲
See also https://(), https://, https://()
bool QList::contains(const T &value) const▲
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▲
See also indexOf(), https://()
int QList::count(const T &value) const▲
Returns the number of occurrences of value in the list.
This function requires the value type to have an implementation of operator==().
See Also▲
See also https://(), indexOf()
int QList::count() const▲
Returns the number of items in the list. This is effectively the same as https://().
bool QList::empty() const▲
This function is provided for STL compatibility. It is equivalent to https:// and returns true if the list is empty.
[since 4.5] bool QList::endsWith(const T &value) const▲
Returns true if this list is not empty and its last item is equal to value; otherwise returns false.
This function was introduced in Qt 4.5.
See Also▲
See also https://, https://()
QList::iterator QList::erase(QList::iterator pos)▲
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▲
See also https://(), https://()
QList::iterator QList::erase(QList::iterator begin, QList::iterator end)▲
This is an overloaded 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.
T &QList::first()▲
Returns a reference to the first item in the list. The list must not be empty. If the list can be empty, call https:// before calling this function.
See Also▲
See also https://(), https://(), https://
const T &QList::first() const▲
This is an overloaded function.
[static] QList<T> QList::fromSet(const QSet<T> &set)▲
Returns a https:// object with the data contained in set. The order of the elements in the https:// is undefined.
Example:
QSet<int> set;
set << 20 << 30 << 40 << ... << 70;
QList<int> list = QList<int>::fromSet(set);
qSort(list);See Also▲
See also https://(), https://(), https://()
[static] QList<T> QList::fromStdList(const std::list<T> &list)▲
Returns a https:// object with the data contained in list. The order of the elements in the https:// 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▲
See also https://(), https://()
[static] QList<T> QList::fromVector(const QVector<T> &vector)▲
Returns a https:// object with the data contained in vector.
Example:
QVector<double> vect;
vect << 20.0 << 30.0 << 40.0 << 50.0;
QList<double> list = QVector<T>::fromVector(vect);
// list: [20.0, 30.0, 40.0, 50.0]See Also▲
See also https://(), https://(), https://()
T &QList::front()▲
This function is provided for STL compatibility. It is equivalent to https://(). The list must not be empty. If the list can be empty, call https:// before calling this function.
const T &QList::front() const▲
This is an overloaded function.
void QList::insert(int i, const T &value)▲
Inserts value at index position i in the list. If i <= 0, the value is prepended to the list. If i >= https://(), 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▲
See also https://(), https://(), https://(), https://()
T &QList::last()▲
Returns a reference to the last item in the list. The list must not be empty. If the list can be empty, call https:// before calling this function.
See Also▲
See also https://(), https://(), https://
const T &QList::last() const▲
This is an overloaded function.
int QList::lastIndexOf(const T &value, int from = -1) const▲
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 -1This function requires the value type to have an implementation of operator==().
Note that https:// uses 0-based indexes, just like C++ arrays. Negative indexes are not supported with the exception of the value mentioned above.
See Also▲
See also indexOf()
[since 4.5] int QList::length() const▲
This function is identical to https://().
This function was introduced in Qt 4.5.
See Also▲
See also https://()
QList<T> QList::mid(int pos, int length = -1) const▲
Returns a sub-list which includes elements from this list, starting at position pos. If length is -1 (the default), all elements from pos are included; otherwise length elements (or all remaining elements if there are less than length elements) are included.
void QList::move(int from, int to)▲
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, https://(from)).This function assumes that both from and to are at least 0 but less than https://(). To avoid failure, test that both from and to are at least 0 and less than https://().
See Also▲
See also swap(), https://(), https://()
void QList::pop_back()▲
This function is provided for STL compatibility. It is equivalent to https://(). The list must not be empty. If the list can be empty, call https:// before calling this function.
void QList::pop_front()▲
This function is provided for STL compatibility. It is equivalent to https://(). The list must not be empty. If the list can be empty, call https:// before calling this function.
void QList::prepend(const T &value)▲
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).
If this list is not shared, this operation is typically very fast (amortized https://), because https:// preallocates extra space on both sides of its internal buffer to allow for fast growth at both ends of the list.
See Also▲
See also https://(), https://()
void QList::push_back(const T &value)▲
This function is provided for STL compatibility. It is equivalent to https://.
void QList::push_front(const T &value)▲
This function is provided for STL compatibility. It is equivalent to https://.
int QList::removeAll(const T &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▲
See also https://(), https://(), https://(), https://()
void QList::removeAt(int i)▲
Removes the item at index position i. i must be a valid index position in the list (i.e., 0 <= i < https://()).
See Also▲
See also https://(), https://(), https://(), https://()
void QList::removeFirst()▲
Removes the first item in the list. Calling this function is equivalent to calling https://(0). The list must not be empty. If the list can be empty, call https:// before calling this function.
See Also▲
See also https://(), https://()
void QList::removeLast()▲
Removes the last item in the list. Calling this function is equivalent to calling https://(https://() - 1). The list must not be empty. If the list can be empty, call https:// before calling this function.
See Also▲
See also https://(), https://()
[since 4.4] bool QList::removeOne(const T &value)▲
Removes the first occurrence of value in the list and returns true on success; otherwise returns false.
Example:
QList<QString> list;
list << "sun" << "cloud" << "sun" << "rain";
list.removeOne("sun");
// list: ["cloud", "sun", "rain"]This function requires the value type to have an implementation of operator==().
This function was introduced in Qt 4.4.
See Also▲
See also https://(), https://(), https://(), https://()
void QList::replace(int i, const T &value)▲
Replaces the item at index position i with value. i must be a valid index position in the list (i.e., 0 <= i < https://()).
See Also▲
See also https://(), https://()
[since 4.7] void QList::reserve(int alloc)▲
Reserve space for alloc elements.
If alloc is smaller than the current size of the list, nothing will happen.
Use this function to avoid repetetive reallocation of https://'s internal data if you can predict how many elements will be appended. Note that the reservation applies only to the internal pointer array.
This function was introduced in Qt 4.7.
int QList::size() const▲
[since 4.5] bool QList::startsWith(const T &value) const▲
Returns true if this list is not empty and its first item is equal to value; otherwise returns false.
This function was introduced in Qt 4.5.
See Also▲
See also https://, https://()
[since 5.13] void QList::swapItemsAt(int i, int j)▲
Exchange the item at index position i with the item at index position j. This function assumes that both i and j are at least 0 but less than https://(). To avoid failure, test that both i and j are at least 0 and less than https://().
Example:
QList<QString> list;
list << "A" << "B" << "C" << "D" << "E" << "F";
list.swapItemsAt(1, 4);
// list: ["A", "E", "C", "D", "B", "F"]This function was introduced in Qt 5.13.
See Also▲
See also https://()
T QList::takeAt(int i)▲
Removes the item at index position i and returns it. i must be a valid index position in the list (i.e., 0 <= i < https://()).
If you don't use the return value, https://() is more efficient.
See Also▲
See also https://(), https://(), https://()
T QList::takeFirst()▲
Removes the first item in the list and returns it. This is the same as https://(0). This function assumes the list is not empty. To avoid failure, call https:// before calling this function.
If this list is not shared, this operation takes https://.
If you don't use the return value, https://() is more efficient.
See Also▲
See also https://(), https://(), https://()
T QList::takeLast()▲
Removes the last item in the list and returns it. This is the same as https://(https://() - 1). This function assumes the list is not empty. To avoid failure, call https:// before calling this function.
If this list is not shared, this operation takes https://.
If you don't use the return value, https://() is more efficient.
See Also▲
See also https://(), https://(), https://()
QSet<T> QList::toSet() const▲
Returns a https:// object with the data contained in this https://. Since https:// doesn't allow duplicates, the resulting https:// 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 2See Also▲
See also https://(), https://(), https://()
std::list<T> QList::toStdList() const▲
Returns a std::list object with the data contained in this https://. Example:
QList<double> list;
list << 1.2 << 0.5 << 3.14;
std::list<double> stdlist = list.toStdList();See Also▲
See also https://(), https://()
QVector<T> QList::toVector() const▲
Returns a https:// object with the data contained in this https://.
Example:
QStringList list;
list << "Sven" << "Kim" << "Ola";
QVector<QString> vect = list.toVector();
// vect: ["Sven", "Kim", "Ola"]See Also▲
See also https://(), https://(), https://()
T QList::value(int i) const▲
Returns the value at index position i in the list.
If the index i is out of bounds, the function returns a https://. If you are certain that the index is going to be within bounds, you can use https://() instead, which is slightly faster.
See Also▲
See also https://(), https://()
T QList::value(int i, const T &defaultValue) const▲
This is an overloaded function.
If the index i is out of bounds, the function returns defaultValue.
bool QList::operator!=(const QList<T> &other) const▲
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▲
See also operator==()
QList<T> QList::operator+(const QList<T> &other) const▲
Returns a list that contains all the items in this list followed by all the items in the other list.
See Also▲
See also https://()
QList<T> &QList::operator+=(const QList<T> &other)▲
Appends the items of the other list to this list and returns a reference to this list.
See Also▲
See also https://(), https://()
QList<T> &QList::operator+=(const T &value)▲
This is an overloaded function.
Appends value to the list.
See Also▲
See also https://(), https://()
QList<T> &QList::operator<<(const QList<T> &other)▲
Appends the items of the other list to this list and returns a reference to this list.
See Also▲
See also https://(), https://()
QList<T> &QList::operator<<(const T &value)▲
This is an overloaded function.
Appends value to the list.
QList<T> &QList::operator=(const QList<T> &other)▲
Assigns other to this list and returns a reference to this list.
[default, since 5.2] int &QList::operator=(QList<T> &&)▲
Move-assigns other to this https:// instance.
This function was introduced in Qt 5.2.
T &QList::operator[](int i)▲
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 < https://()).
If this function is called on a list that is currently being shared, it will trigger a copy of all elements. Otherwise, this function runs in https://. If you do not want to modify the list you should use https://().
See Also▲
See also https://(), https://()
const T &QList::operator[](int i) const▲
This is an overloaded function.
Same as https://(). This function runs in https://.
Related Non-Members▲
QDataStream &operator<<(QDataStream &out, const QList<T> &list)▲
Writes the list list to stream out.
This function requires the value type to implement operator<<().
See Also▲
See also https://
QDataStream &operator>>(QDataStream &in, QList<T> &list)▲
Reads a list from stream in into list.
This function requires the value type to implement operator>>().
See Also▲
See also https://


