QList Class
template <typename T> class QListThe QList class is a template class that provides a dynamic array. More...
Header: | #include <QList> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
Inherited By: |
- List of all members, including inherited members
- Deprecated members
- QList is part of Implicitly Shared Classes.
Note: All functions in this class are reentrant.
Public Types
Public Functions
QList() | |
QList(qsizetype size) | |
QList(std::initializer_list<T> args) | |
QList(InputIterator first, InputIterator last) | |
QList(qsizetype size, QList<T>::parameter_type value) | |
(since 6.8) | QList(qsizetype size, Qt::Initialization) |
QList(const QList<T> &other) | |
QList(QList<T> &&other) | |
~QList() | |
void | append(QList<T>::parameter_type value) |
(since 6.0) void | append(QList<T> &&value) |
void | append(QList<T>::rvalue_ref value) |
void | append(const QList<T> &value) |
(since 6.6) QList<T> & | assign(std::initializer_list<T> l) |
(since 6.6) QList<T> & | assign(InputIterator first, InputIterator last) |
(since 6.6) QList<T> & | assign(qsizetype n, QList<T>::parameter_type t) |
QList<T>::const_reference | at(qsizetype i) const |
QList<T>::reference | back() |
QList<T>::const_reference | back() const |
QList<T>::iterator | begin() |
QList<T>::const_iterator | begin() const |
qsizetype | capacity() const |
QList<T>::const_iterator | cbegin() const |
QList<T>::const_iterator | cend() const |
void | clear() |
QList<T>::const_iterator | constBegin() const |
QList<T>::const_pointer | constData() const |
QList<T>::const_iterator | constEnd() const |
const T & | constFirst() const |
const T & | constLast() const |
bool | contains(const AT &value) const |
qsizetype | count(const AT &value) const |
qsizetype | count() const |
QList<T>::const_reverse_iterator | crbegin() const |
QList<T>::const_reverse_iterator | crend() const |
QList<T>::pointer | data() |
QList<T>::const_pointer | data() const |
QList<T>::iterator | emplace(qsizetype i, Args &&... args) |
QList<T>::iterator | emplace(QList<T>::const_iterator before, Args &&... args) |
QList<T>::reference | emplaceBack(Args &&... args) |
QList<T>::reference | emplace_back(Args &&... args) |
bool | empty() const |
QList<T>::iterator | end() |
QList<T>::const_iterator | end() const |
bool | endsWith(QList<T>::parameter_type value) const |
QList<T>::iterator | erase(QList<T>::const_iterator pos) |
QList<T>::iterator | erase(QList<T>::const_iterator begin, QList<T>::const_iterator end) |
QList<T> & | fill(QList<T>::parameter_type value, qsizetype size = -1) |
T & | first() |
(since 6.0) QList<T> | first(qsizetype n) const |
const T & | first() const |
QList<T>::reference | front() |
QList<T>::const_reference | front() const |
qsizetype | indexOf(const AT &value, qsizetype from = 0) const |
QList<T>::iterator | insert(qsizetype i, QList<T>::parameter_type value) |
QList<T>::iterator | insert(qsizetype i, QList<T>::rvalue_ref value) |
QList<T>::iterator | insert(QList<T>::const_iterator before, qsizetype count, QList<T>::parameter_type value) |
QList<T>::iterator | insert(QList<T>::const_iterator before, QList<T>::parameter_type value) |
QList<T>::iterator | insert(QList<T>::const_iterator before, QList<T>::rvalue_ref value) |
QList<T>::iterator | insert(qsizetype i, qsizetype count, QList<T>::parameter_type value) |
bool | isEmpty() const |
T & | last() |
(since 6.0) QList<T> | last(qsizetype n) const |
const T & | last() const |
qsizetype | lastIndexOf(const AT &value, qsizetype from = -1) const |
qsizetype | length() const |
(since 6.8) qsizetype | max_size() const |
QList<T> | mid(qsizetype pos, qsizetype length = -1) const |
void | move(qsizetype from, qsizetype to) |
void | pop_back() |
void | pop_front() |
void | prepend(QList<T>::parameter_type value) |
void | prepend(QList<T>::rvalue_ref value) |
void | push_back(QList<T>::parameter_type value) |
void | push_back(QList<T>::rvalue_ref value) |
void | push_front(QList<T>::parameter_type value) |
void | push_front(QList<T>::rvalue_ref value) |
QList<T>::reverse_iterator | rbegin() |
QList<T>::const_reverse_iterator | rbegin() const |
void | remove(qsizetype i, qsizetype n = 1) |
qsizetype | removeAll(const AT &t) |
void | removeAt(qsizetype i) |
void | removeFirst() |
(since 6.1) qsizetype | removeIf(Predicate pred) |
void | removeLast() |
bool | removeOne(const AT &t) |
QList<T>::reverse_iterator | rend() |
QList<T>::const_reverse_iterator | rend() const |
void | replace(qsizetype i, QList<T>::parameter_type value) |
void | replace(qsizetype i, QList<T>::rvalue_ref value) |
void | reserve(qsizetype size) |
(since 6.0) void | resize(qsizetype size) |
(since 6.0) void | resize(qsizetype size, QList<T>::parameter_type c) |
(since 6.8) void | resizeForOverwrite(qsizetype size) |
void | shrink_to_fit() |
qsizetype | size() const |
(since 6.0) QList<T> | sliced(qsizetype pos, qsizetype n) const |
(since 6.0) QList<T> | sliced(qsizetype pos) const |
void | squeeze() |
bool | startsWith(QList<T>::parameter_type value) const |
void | swap(QList<T> &other) |
void | swapItemsAt(qsizetype i, qsizetype j) |
T | takeAt(qsizetype i) |
QList<T>::value_type | takeFirst() |
QList<T>::value_type | takeLast() |
T | value(qsizetype i) const |
T | value(qsizetype i, QList<T>::parameter_type defaultValue) const |
bool | operator!=(const QList<T> &other) const |
QList<T> | operator+(QList<T> &&other) && |
QList<T> | operator+(const QList<T> &other) && |
QList<T> | operator+(QList<T> &&other) const & |
QList<T> | operator+(const QList<T> &other) const & |
QList<T> & | operator+=(const QList<T> &other) |
(since 6.0) QList<T> & | operator+=(QList<T> &&other) |
QList<T> & | operator+=(QList<T>::parameter_type value) |
QList<T> & | operator+=(QList<T>::rvalue_ref value) |
bool | operator<(const QList<T> &other) const |
QList<T> & | operator<<(QList<T>::parameter_type value) |
QList<T> & | operator<<(const QList<T> &other) |
(since 6.0) QList<T> & | operator<<(QList<T> &&other) |
QList<T> & | operator<<(QList<T>::rvalue_ref value) |
bool | operator<=(const QList<T> &other) const |
QList<T> & | operator=(QList<T> &&other) |
QList<T> & | operator=(const QList<T> &other) |
QList<T> & | operator=(std::initializer_list<T> args) |
bool | operator==(const QList<T> &other) const |
bool | operator>(const QList<T> &other) const |
bool | operator>=(const QList<T> &other) const |
QList<T>::reference | operator[](qsizetype i) |
QList<T>::const_reference | operator[](qsizetype i) const |
Static Public Members
(since 6.8) qsizetype | maxSize() |
Related Non-Members
(since 6.1) qsizetype | erase(QList<T> &list, const AT &t) |
(since 6.1) qsizetype | erase_if(QList<T> &list, Predicate pred) |
size_t | qHash(const QList<T> &key, size_t seed = 0) |
QDataStream & | operator<<(QDataStream &out, const QList<T> &list) |
QDataStream & | operator>>(QDataStream &in, QList<T> &list) |
Detailed Description
QList<T> is one of Qt's generic container classes. It stores its items in adjacent memory locations and provides fast index-based access. QVector<T> used to be a different class in Qt 5, but is now a simple alias to QList.
QList<T> and QVarLengthArray<T> provide similar APIs and functionality. They are often interchangeable, but there are performance consequences. Here is an overview of use cases:
- QList should be your default first choice.
- QVarLengthArray provides an array that reserves space on the stack, but can dynamically grow onto the heap if required. It's good to use for short lived containers that are usually small.
- If you need a real linked list, which guarantees constant time insertions mid-list and uses iterators to items rather than indexes, use std::list.
Note: QList and QVarLengthArray both guarantee C-compatible array layout.
Note: QList in Qt 5 did not always have a C-compatible array layout and we often recommended to use QVector instead for more predictable performance. This is not the case in Qt 6 anymore, where both classes now share an implementation and can be used interchangeably.
Here's an example of a QList that stores integers and a QList that stores QString values:
QList stores its items in an array of continuous memory. Typically, lists are created with an initial size. For example, the following code constructs a QList with 200 elements:
The elements are automatically initialized with a default-constructed value. If you want to initialize the list with a different value, pass that value as the second argument to the constructor:
You can also call fill() at any time to fill the list with a value.
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 that can be used on the left side of an assignment:
if (list[0] == "Liz") list[0] = "Elizabeth";
For read-only access, an alternative syntax is to use at():
for (qsizetype i = 0; i < list.size(); ++i) { if (list.at(i) == "Alfonso") cout << "Found Alfonso at position " << i << endl; }
at() can be faster than operator[](), because it never causes a deep copy to occur.
Another way to access the data stored in a QList is to call data(). The function returns a pointer to the first item in the list. You can use the pointer to directly access and modify the elements stored in the list. The pointer is also useful if you need to pass a QList to a function that accepts a plain C++ array.
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 the matching item if they found one; otherwise, they return -1. For example:
qsizetype i = list.indexOf("Harumi"); if (i != -1) cout << "First occurrence of Harumi 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().
QList provides these basic functions to add, move, and remove items: insert(), replace(), remove(), prepend(), append(). With the exception of append(), prepend() and replace(), these functions can be slow (linear time) for large lists, because they require moving many items in the list by one position in memory. If you want a container class that provides fast insertion/removal in the middle, use std::list instead.
Unlike plain C++ arrays, QLists can be resized at any time by calling resize(). If the new size is larger than the old size, QList might need to reallocate the whole list. QList tries to reduce the number of reallocations by preallocating up to twice as much memory as the actual data needs.
If you're building a QList gradually and know in advance approximately how many elements it will contain, you can call reserve(), asking QList to preallocate a certain amount of memory. You can also call capacity() to find out how much memory the QList actually has allocated.
Note that using non-const operators and functions can cause QList to do a deep copy of the data, due to implicit sharing.
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.
For iterating over the items, see Iterating over Containers. For using QList with functions from <algorithm>
header, such as std::sort()
, std::reverse()
, and std::count_if()
, see Qt containers and std algorithms.
In addition to QList, Qt also provides QVarLengthArray, a very low-level class with little functionality that is optimized for speed.
More Information on Using Qt Containers
For a detailed discussion comparing Qt containers with each other and with STL containers, see Understand the Qt Containers.
Maximum size and out-of-memory conditions
The maximum size of QList depends on the architecture. Most 64-bit systems can allocate more than 2 GB of memory, with a typical limit of 2^63 bytes. The actual value also depends on the overhead required for managing the data block. As a result, you can expect the maximum size of 2 GB minus overhead on 32-bit platforms, and 2^63 bytes minus overhead on 64-bit platforms. The number of elements that can be stored in a QList is this maximum size divided by the size of a stored element.
When memory allocation fails, QList uses the Q_CHECK_PTR macro, which throws a std::bad_alloc
exception if the application is being compiled with exception support. If exceptions are disabled, then running out of memory is undefined behavior.
Note that the operating system may impose further limits on applications holding a lot of allocated memory, especially large, contiguous blocks. Such considerations, the configuration of such behavior or any mitigation are outside the scope of the Qt API.
Member Type Documentation
[alias]
QList::ConstIterator
Qt-style synonym for QList::const_iterator.
[alias]
QList::Iterator
Qt-style synonym for QList::iterator.
[alias]
QList::const_pointer
Provided for STL compatibility.
[alias]
QList::const_reference
Provided for STL compatibility.
[alias]
QList::const_reverse_iterator
The QList::const_reverse_iterator typedef provides an STL-style const reverse iterator for QList.
Warning: 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 Implicit sharing iterator problem.
Warning: Iterators are invalidated when QList is modified. Consider that all iterators are invalidated by default. Exceptions to this rule are explicitly documented.
See also QList::rbegin(), QList::rend(), QList::reverse_iterator, and QList::const_iterator.
[alias]
QList::difference_type
Provided for STL compatibility.
[alias]
QList::parameter_type
[alias]
QList::pointer
Provided for STL compatibility.
[alias]
QList::reference
Provided for STL compatibility.
[alias]
QList::reverse_iterator
The QList::reverse_iterator typedef provides an STL-style non-const reverse iterator for QList.
Warning: 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 Implicit sharing iterator problem.
Warning: Iterators are invalidated when QList is modified. Consider that all iterators are invalidated by default. Exceptions to this rule are explicitly documented.
See also QList::rbegin(), QList::rend(), QList::const_reverse_iterator, and QList::iterator.
[alias]
QList::rvalue_ref
[alias]
QList::size_type
Provided for STL compatibility.
[alias]
QList::value_type
Provided for STL compatibility.
Member Function Documentation
[since 6.0]
void QList::resize(qsizetype size)
[since 6.0]
void QList::resize(qsizetype size, QList<T>::parameter_type c)
Sets the size of the list to size. If size is greater than the current size, elements are added to the end; the new elements are initialized with either a default-constructed value or c. If size is less than the current size, elements are removed from the end.
If this list is not shared, the capacity() is preserved. Use squeeze() to shed excess capacity.
Note: In Qt versions prior to 5.7 (for QVector; QList lacked a resize() until 6.0), this function released the memory used by the list instead of preserving the capacity.
This function was introduced in Qt 6.0.
See also size().
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).
Normally this operation is relatively fast (amortized constant time). QList is able to allocate extra memory at the beginning of the list data and grow in that direction without reallocating or moving the data on each operation. However if you want a container class with a guarantee of constant time prepend, use std::list instead, but prefer QList otherwise.
See also append() and insert().
template <typename... Args> QList<T>::reference QList::emplaceBack(Args &&... args)
template <typename... Args> QList<T>::reference QList::emplace_back(Args &&... args)
Adds a new element to the end for the container. This new element is constructed in-place using args as the arguments for its construction.
Returns a reference to the new element.
Example:
QList<QString> list{"one", "two"}; list.emplaceBack(3, 'a'); qDebug() << list; // list: ["one", "two", "aaa"]
It is also possible to access a newly created object by using returned reference:
This is the same as list.emplace(list.size(), args).
See also emplace.
QList<T>::iterator QList::insert(qsizetype i, QList<T>::parameter_type value)
QList<T>::iterator QList::insert(qsizetype i, QList<T>::rvalue_ref value)
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 = {"alpha", "beta", "delta"}; list.insert(2, "gamma"); // list: ["alpha", "beta", "gamma", "delta"]
For large lists, this operation can be slow (linear time), because it requires moving all the items at indexes i and above by one position further in memory. If you want a container class that provides a fast insert() function, use std::list instead.
See also append(), prepend(), and remove().
QList<T>::iterator QList::insert(QList<T>::const_iterator before, QList<T>::parameter_type value)
QList<T>::iterator QList::insert(QList<T>::const_iterator before, QList<T>::rvalue_ref value)
This is an overloaded function.
Inserts value in front of the item pointed to by the iterator before. Returns an iterator pointing at the inserted item.
void QList::replace(qsizetype i, QList<T>::parameter_type value)
void QList::replace(qsizetype i, QList<T>::rvalue_ref value)
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 remove().
void QList::push_front(QList<T>::parameter_type value)
void QList::push_front(QList<T>::rvalue_ref value)
This function is provided for STL compatibility. It is equivalent to prepend(value).
[static constexpr, since 6.8]
qsizetype QList::maxSize()
[constexpr noexcept, since 6.8]
qsizetype QList::max_size() const
It returns the maximum number of elements that the list can theoretically hold. In practice, the number can be much smaller, limited by the amount of memory available to the system.
This function was introduced in Qt 6.8.
QList<T> QList::operator+(QList<T> &&other) &&
QList<T> QList::operator+(const QList<T> &other) &&
QList<T> QList::operator+(QList<T> &&other) const &
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 operator+=().
QList::QList()
Constructs an empty list.
See also resize().
[explicit]
QList::QList(qsizetype size)
Constructs a list with an initial size of size elements.
The elements are initialized with a default-constructed value.
See also resize().
QList::QList(std::initializer_list<T> args)
Constructs a list from the std::initializer_list given by args.
template <typename InputIterator, QList<T>::if_input_iterator<InputIterator> = true> QList::QList(InputIterator first, InputIterator last)
Constructs a list with the contents in the iterator range [first, last).
Note: This constructor only participates in overload resolution if InputIterator
meets the requirements of a LegacyInputIterator.
The value type of InputIterator
must be convertible to T
.
QList::QList(qsizetype size, QList<T>::parameter_type value)
Constructs a list with an initial size of size elements. Each element is initialized with value.
[since 6.8]
QList::QList(qsizetype size, Qt::Initialization)
Constructs a list with an initial size of size elements.
QList will make an attempt at not initializing the elements.
Specifically:
- if
T
has a constructor that acceptsQt::Uninitialized
, that constructor will be used to initialize the elements; - otherwise, each element is default constructed. For trivially constructible types (such as
int
,float
, etc.) this is equivalent to not initializing them.
This function was introduced in Qt 6.8.
See also resizeForOverwrite().
[default]
QList::QList(const QList<T> &other)
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=().
[default]
QList::QList(QList<T> &&other)
Move-constructs a QList instance, making it point at the same object that other was pointing to.
[default]
QList::~QList()
Destroys the list.
void QList::append(QList<T>::parameter_type value)
Inserts value at the end of the list.
Example:
QList<QString> list; list.append("one"); list.append("two"); QString three = "three"; list.append(three); // list: ["one", "two", "three"] // three: "three"
This is the same as calling resize(size() + 1) and assigning value to the new last element in the list.
This operation is relatively fast, because QList typically allocates more memory than necessary, so it can grow without reallocating the entire list each time.
See also operator<<(), prepend(), and insert().
[since 6.0]
void QList::append(QList<T> &&value)
This is an overloaded function.
Moves the items of the value list to the end of this list.
This function was introduced in Qt 6.0.
See also operator<<() and operator+=().
void QList::append(QList<T>::rvalue_ref value)
This is an overloaded function.
Example:
QList<QString> list; list.append("one"); list.append("two"); QString three = "three"; list.append(std::move(three)); // list: ["one", "two", "three"] // three: ""
void QList::append(const QList<T> &value)
This is an overloaded function.
Appends the items of the value list to this list.
See also operator<<() and operator+=().
[since 6.6]
QList<T> &QList::assign(std::initializer_list<T> l)
Replaces the contents of this list with a copy of the elements of l.
The size of this list will be equal to the number of elements in l.
This function only allocates memory if the number of elements in l exceeds the capacity of this list or this list is shared.
This function was introduced in Qt 6.6.
[since 6.6]
template <typename InputIterator, QList<T>::if_input_iterator<InputIterator> = true> QList<T> &QList::assign(InputIterator first, InputIterator last)
Replaces the contents of this list with a copy of the elements in the iterator range [first, last).
The size of this list will be equal to the number of elements in the range [first, last).
This function will only allocate memory if the number of elements in the range exceeds the capacity of this list or this list is shared.
Note: This function overload only participates in overload resolution if InputIterator
meets the requirements of a LegacyInputIterator.
Note: The behavior is undefined if either argument is an iterator into *this.
This function was introduced in Qt 6.6.
[since 6.6]
QList<T> &QList::assign(qsizetype n, QList<T>::parameter_type t)
Replaces the contents of this list with n copies of t.
The size of this list will be equal to n.
This function will only allocate memory if n exceeds the capacity of the list or this list is shared.
This function was introduced in Qt 6.6.
[noexcept]
QList<T>::const_reference QList::at(qsizetype 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 < size()).
See also value() and operator[]().
QList<T>::reference QList::back()
This function is provided for STL compatibility. It is equivalent to last().
[noexcept]
QList<T>::const_reference QList::back() const
This is an overloaded function.
QList<T>::iterator QList::begin()
Returns an STL-style iterator pointing to the first item in the list.
Warning: The returned iterator is invalidated on detachment or when the QList is modified.
See also constBegin() and end().
[noexcept]
QList<T>::const_iterator QList::begin() const
This is an overloaded function.
qsizetype QList::capacity() const
Returns the maximum number of items that can be stored in the list without forcing a reallocation.
The sole purpose of this function is to provide a means of fine tuning QList's memory usage. In general, you will rarely ever need to call this function. If you want to know how many items are in the list, call size().
Note: a statically allocated list will report a capacity of 0, even if it's not empty.
Warning: The free space position in the allocated memory block is undefined. In other words, you should not assume that the free memory is always located at the end of the list. You can call reserve() to ensure that there is enough space at the end.
See also reserve() and squeeze().
[noexcept]
QList<T>::const_iterator QList::cbegin() const
Returns a const STL-style iterator pointing to the first item in the list.
Warning: The returned iterator is invalidated on detachment or when the QList is modified.
[noexcept]
QList<T>::const_iterator QList::cend() const
Returns a const STL-style iterator pointing just after the last item in the list.
Warning: The returned iterator is invalidated on detachment or when the QList is modified.
void QList::clear()
Removes all the elements from the list.
If this list is not shared, the capacity() is preserved. Use squeeze() to shed excess capacity.
Note: In Qt versions prior to 5.7 (for QVector) and 6.0 (for QList), this function released the memory used by the list instead of preserving the capacity.
See also resize() and squeeze().
[noexcept]
QList<T>::const_iterator QList::constBegin() const
Returns a const STL-style iterator pointing to the first item in the list.
Warning: The returned iterator is invalidated on detachment or when the QList is modified.
See also begin() and constEnd().
[noexcept]
QList<T>::const_pointer QList::constData() const
Returns a const pointer to the data stored in the list. The pointer can be used to access the items in the list.
Warning: The pointer is invalidated on detachment or when the QList is modified.
This function is mostly useful to pass a list to a function that accepts a plain C++ array.
See also data() and operator[]().
[noexcept]
QList<T>::const_iterator QList::constEnd() const
Returns a const STL-style iterator pointing just after the last item in the list.
Warning: The returned iterator is invalidated on detachment or when the QList is modified.
See also constBegin() and end().
[noexcept]
const T &QList::constFirst() const
Returns a const reference to the first item in the list. This function assumes that the list isn't empty.
See also constLast(), isEmpty(), and first().
[noexcept]
const T &QList::constLast() const
Returns a const reference to the last item in the list. This function assumes that the list isn't empty.
See also constFirst(), isEmpty(), and last().
[noexcept]
template <typename AT> bool QList::contains(const AT &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 indexOf() and count().
[noexcept]
template <typename AT = T> qsizetype QList::count(const AT &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 contains() and indexOf().
[noexcept]
qsizetype QList::count() const
This is an overloaded function.
Same as size().
[noexcept]
QList<T>::const_reverse_iterator QList::crbegin() const
Returns a const STL-style reverse iterator pointing to the first item in the list, in reverse order.
Warning: The returned iterator is invalidated on detachment or when the QList is modified.
See also begin(), rbegin(), and rend().
[noexcept]
QList<T>::const_reverse_iterator QList::crend() const
Returns a const STL-style reverse iterator pointing just after the last item in the list, in reverse order.
Warning: The returned iterator is invalidated on detachment or when the QList is modified.
See also end(), rend(), and rbegin().
QList<T>::pointer QList::data()
Returns a pointer to the data stored in the list. The pointer can be used to access and modify the items in the list.
Example:
QList<int> list(10); int *data = list.data(); for (qsizetype i = 0; i < 10; ++i) data[i] = 2 * i;
Warning: The pointer is invalidated on detachment or when the QList is modified.
This function is mostly useful to pass a list to a function that accepts a plain C++ array.
See also constData() and operator[]().
[noexcept]
QList<T>::const_pointer QList::data() const
This is an overloaded function.
template <typename... Args> QList<T>::iterator QList::emplace(qsizetype i, Args &&... args)
Extends the container by inserting a new element at position i. This new element is constructed in-place using args as the arguments for its construction.
Returns an iterator to the new element.
Example:
Note: It is guaranteed that the element will be created in place at the beginning, but after that it might be copied or moved to the right position.
See also emplaceBack.
template <typename... Args> QList<T>::iterator QList::emplace(QList<T>::const_iterator before, Args &&... args)
This is an overloaded function.
Creates a new element in front of the item pointed to by the iterator before. This new element is constructed in-place using args as the arguments for its construction.
Returns an iterator to the new element.
[noexcept]
bool QList::empty() const
This function is provided for STL compatibility. It is equivalent to isEmpty(), returning true
if the list is empty; otherwise returns false
.
QList<T>::iterator QList::end()
Returns an STL-style iterator pointing just after the last item in the list.
Warning: The returned iterator is invalidated on detachment or when the QList is modified.
See also begin() and constEnd().
[noexcept]
QList<T>::const_iterator QList::end() const
This is an overloaded function.
bool QList::endsWith(QList<T>::parameter_type value) const
Returns true
if this list is not empty and its last item is equal to value; otherwise returns false
.
See also isEmpty() and last().
QList<T>::iterator QList::erase(QList<T>::const_iterator pos)
Removes the item pointed to by the iterator pos from the list, and returns an iterator to the next item in the list (which may be end()).
Element removal will preserve the list's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze().
Note: When QList is not implicitly shared, this function only invalidates iterators at or after the specified position.
See also insert() and remove().
QList<T>::iterator QList::erase(QList<T>::const_iterator begin, QList<T>::const_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.
Element removal will preserve the list's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze().
Note: When QList is not implicitly shared, this function only invalidates iterators at or after the specified position.
QList<T> &QList::fill(QList<T>::parameter_type value, qsizetype size = -1)
Assigns value to all items in the list. If size is different from -1 (the default), the list is resized to size beforehand.
Example:
QList<QString> list(3); list.fill("Yes"); // list: ["Yes", "Yes", "Yes"] list.fill("oh", 5); // list: ["oh", "oh", "oh", "oh", "oh"]
See also resize().
T &QList::first()
Returns a reference to the first item in the list. This function assumes that the list isn't empty.
See also last(), isEmpty(), and constFirst().
[since 6.0]
QList<T> QList::first(qsizetype n) const
Returns a sub-list that contains the first n elements of this list.
Note: The behavior is undefined when n < 0 or n > size().
This function was introduced in Qt 6.0.
[noexcept]
const T &QList::first() const
This is an overloaded function.
QList<T>::reference QList::front()
This function is provided for STL compatibility. It is equivalent to first().
[noexcept]
QList<T>::const_reference QList::front() const
This is an overloaded function.
[noexcept]
template <typename AT> qsizetype QList::indexOf(const AT &value, qsizetype from = 0) const
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{"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().
QList<T>::iterator QList::insert(QList<T>::const_iterator before, qsizetype count, QList<T>::parameter_type value)
Inserts count copies of value in front of the item pointed to by the iterator before. Returns an iterator pointing at the first of the inserted items.
QList<T>::iterator QList::insert(qsizetype i, qsizetype count, QList<T>::parameter_type value)
This is an overloaded function.
Inserts count copies of value at index position i in the list.
Example:
QList<double> list = {2.718, 1.442, 0.4342}; list.insert(1, 3, 9.9); // list: [2.718, 9.9, 9.9, 9.9, 1.442, 0.4342]
[noexcept]
bool QList::isEmpty() const
Returns true
if the list has size 0; otherwise returns false
.
T &QList::last()
Returns a reference to the last item in the list. This function assumes that the list isn't empty.
See also first(), isEmpty(), and constLast().
[since 6.0]
QList<T> QList::last(qsizetype n) const
Returns a sub-list that contains the last n elements of this list.
Note: The behavior is undefined when n < 0 or n > size().
This function was introduced in Qt 6.0.
See also first() and sliced().
[noexcept]
const T &QList::last() const
This is an overloaded function.
[noexcept]
template <typename AT> qsizetype QList::lastIndexOf(const AT &value, qsizetype from = -1) const
Returns the index position of the last occurrence of the value 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 = {"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().
[noexcept]
qsizetype QList::length() const
QList<T> QList::mid(qsizetype pos, qsizetype length = -1) const
Returns a sub-list which contains elements from this list, starting at position pos. If length is -1 (the default), all elements after pos are included; otherwise length elements (or all remaining elements if there are less than length elements) are included.
void QList::move(qsizetype from, qsizetype to)
Moves the item at index position from to index position to.
[noexcept]
void QList::pop_back()
This function is provided for STL compatibility. It is equivalent to removeLast().
[noexcept]
void QList::pop_front()
This function is provided for STL compatibility. It is equivalent to removeFirst().
void QList::push_back(QList<T>::parameter_type value)
This function is provided for STL compatibility. It is equivalent to append(value).
void QList::push_back(QList<T>::rvalue_ref value)
This is an overloaded function.
QList<T>::reverse_iterator QList::rbegin()
Returns a STL-style reverse iterator pointing to the first item in the list, in reverse order.
Warning: The returned iterator is invalidated on detachment or when the QList is modified.
See also begin(), crbegin(), and rend().
[noexcept]
QList<T>::const_reverse_iterator QList::rbegin() const
This is an overloaded function.
void QList::remove(qsizetype i, qsizetype n = 1)
Removes n elements from the list, starting at index position i.
Element removal will preserve the list's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze().
Note: When QList is not implicitly shared, this function only invalidates iterators at or after the specified position.
See also insert(), replace(), and fill().
template <typename AT = T> qsizetype QList::removeAll(const AT &t)
Removes all elements that compare equal to t from the list. Returns the number of elements removed, if any.
Element removal will preserve the list's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze().
See also removeOne().
void QList::removeAt(qsizetype i)
Removes the element at index position i. Equivalent to
remove(i);
Element removal will preserve the list's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze().
Note: When QList is not implicitly shared, this function only invalidates iterators at or after the specified position.
See also remove().
[noexcept]
void QList::removeFirst()
Removes the first item in the list. Calling this function is equivalent to calling remove(0). The list must not be empty. If the list can be empty, call isEmpty() before calling this function.
Element removal will preserve the list's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze().
See also remove(), takeFirst(), and isEmpty().
[since 6.1]
template <typename Predicate> qsizetype QList::removeIf(Predicate pred)
Removes all elements for which the predicate pred returns true from the list. Returns the number of elements removed, if any.
This function was introduced in Qt 6.1.
See also removeAll().
[noexcept]
void QList::removeLast()
Removes the last item in the list. Calling this function is equivalent to calling remove(size() - 1). The list must not be empty. If the list can be empty, call isEmpty() before calling this function.
Element removal will preserve the list's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze().
See also remove(), takeLast(), removeFirst(), and isEmpty().
template <typename AT = T> bool QList::removeOne(const AT &t)
Removes the first element that compares equal to t from the list. Returns whether an element was, in fact, removed.
Element removal will preserve the list's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze().
See also removeAll().
QList<T>::reverse_iterator QList::rend()
Returns a STL-style reverse iterator pointing just after the last item in the list, in reverse order.
Warning: The returned iterator is invalidated on detachment or when the QList is modified.
See also end(), crend(), and rbegin().
[noexcept]
QList<T>::const_reverse_iterator QList::rend() const
This is an overloaded function.
void QList::reserve(qsizetype size)
Attempts to allocate memory for at least size elements.
If you know in advance how large the list will be, you should call this function to prevent reallocations and memory fragmentation. If you resize the list often, you are also likely to get better performance.
If in doubt about how much space shall be needed, it is usually better to use an upper bound as size, or a high estimate of the most likely size, if a strict upper bound would be much bigger than this. If size is an underestimate, the list will grow as needed once the reserved size is exceeded, which may lead to a larger allocation than your best overestimate would have and will slow the operation that triggers it.
Warning: reserve() reserves memory but does not change the size of the list. Accessing data beyond the current end of the list is undefined behavior. If you need to access memory beyond the current end of the list, use resize().
See also squeeze(), capacity(), and resize().
[since 6.8]
void QList::resizeForOverwrite(qsizetype size)
Sets the size of the list to size. If size is less than the current size, elements are removed from the end. If size is greater than the current size, elements are added to the end; QList will make an attempt at not initializing these new elements.
Specifically:
- if
T
has a constructor that acceptsQt::Uninitialized
, that constructor will be used to initialize the elements; - otherwise, each element is default constructed. For trivially constructible types (such as
int
,float
, etc.) this is equivalent to not initializing them.
This function was introduced in Qt 6.8.
void QList::shrink_to_fit()
This function is provided for STL compatibility. It is equivalent to squeeze().
[noexcept]
qsizetype QList::size() const
Returns the number of items in the list.
See also isEmpty() and resize().
[since 6.0]
QList<T> QList::sliced(qsizetype pos, qsizetype n) const
Returns a sub-list that contains n elements of this list, starting at position pos.
Note: The behavior is undefined when pos < 0, n < 0, or pos + n > size().
This function was introduced in Qt 6.0.
[since 6.0]
QList<T> QList::sliced(qsizetype pos) const
This is an overloaded function.
Returns a sub-list that contains the elements of this list starting at position pos and extending to its end.
Note: The behavior is undefined when pos < 0 or pos > size().
This function was introduced in Qt 6.0.
void QList::squeeze()
Releases any memory not required to store the items.
The sole purpose of this function is to provide a means of fine tuning QList's memory usage. In general, you will rarely ever need to call this function.
See also reserve() and capacity().
bool QList::startsWith(QList<T>::parameter_type value) const
Returns true
if this list is not empty and its first item is equal to value; otherwise returns false
.
See also isEmpty() and first().
[noexcept]
void QList::swap(QList<T> &other)
Swaps this list with other. This operation is very fast and never fails.
void QList::swapItemsAt(qsizetype i, qsizetype 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 size(). To avoid failure, test that both i and j are at least 0 and less than size().
T QList::takeAt(qsizetype i)
Removes the element at index position i and returns it.
Equivalent to
T t = at(i); remove(i); return t;
Note: When QList is not implicitly shared, this function only invalidates iterators at or after the specified position.
See also takeFirst() and takeLast().
QList<T>::value_type QList::takeFirst()
Removes the first item in the list and returns it. This function assumes the list is not empty. To avoid failure, call isEmpty() before calling this function.
See also takeLast() and removeFirst().
QList<T>::value_type QList::takeLast()
Removes the last item in the list and returns it. This function assumes the list is not empty. To avoid failure, call isEmpty() before calling this function.
If you don't use the return value, removeLast() is more efficient.
See also takeFirst() and removeLast().
T QList::value(qsizetype i) const
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 i is within bounds, you can use at() instead, which is slightly faster.
See also at() and operator[]().
T QList::value(qsizetype i, QList<T>::parameter_type 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 operator==().
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 operator+() and append().
[since 6.0]
QList<T> &QList::operator+=(QList<T> &&other)
This is an overloaded function.
This function was introduced in Qt 6.0.
See also operator+() and append().
QList<T> &QList::operator+=(QList<T>::parameter_type value)
This is an overloaded function.
Appends value to the list.
See also append() and operator<<().
QList<T> &QList::operator+=(QList<T>::rvalue_ref value)
This is an overloaded function.
See also append() and operator<<().
bool QList::operator<(const QList<T> &other) const
Returns true
if this list is lexically less than other; otherwise returns false
.
This function requires the value type to have an implementation of operator<()
.
QList<T> &QList::operator<<(QList<T>::parameter_type value)
Appends value to the list and returns a reference to this list.
See also append() and operator+=().
QList<T> &QList::operator<<(const QList<T> &other)
Appends other to the list and returns a reference to the list.
[since 6.0]
QList<T> &QList::operator<<(QList<T> &&other)
This is an overloaded function.
This function was introduced in Qt 6.0.
QList<T> &QList::operator<<(QList<T>::rvalue_ref value)
This is an overloaded function.
See also append() and operator+=().
bool QList::operator<=(const QList<T> &other) const
Returns true
if this list is lexically less than or equal to other; otherwise returns false
.
This function requires the value type to have an implementation of operator<()
.
[default]
QList<T> &QList::operator=(QList<T> &&other)
Move-assigns other to this QList instance.
[default]
QList<T> &QList::operator=(const QList<T> &other)
Assigns other to this list and returns a reference to this list.
QList<T> &QList::operator=(std::initializer_list<T> args)
Assigns the collection of values in args to this QList instance.
bool QList::operator==(const QList<T> &other) const
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!=().
bool QList::operator>(const QList<T> &other) const
Returns true
if this list is lexically greater than other; otherwise returns false
.
This function requires the value type to have an implementation of operator<()
.
bool QList::operator>=(const QList<T> &other) const
Returns true
if this list is lexically greater than or equal to other; otherwise returns false
.
This function requires the value type to have an implementation of operator<()
.
QList<T>::reference QList::operator[](qsizetype 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 < size()).
Note that using non-const operators can cause QList to do a deep copy.
[noexcept]
QList<T>::const_reference QList::operator[](qsizetype i) const
This is an overloaded function.
Same as at(i).
Related Non-Members
[since 6.1]
template <typename T, typename AT> qsizetype erase(QList<T> &list, const AT &t)
Removes all elements that compare equal to t from the list list. Returns the number of elements removed, if any.
Note: Unlike QList::removeAll, t is not allowed to be a reference to an element inside list. If you cannot be sure that this is not the case, take a copy of t and call this function with the copy.
This function was introduced in Qt 6.1.
See also QList::removeAll() and erase_if.
[since 6.1]
template <typename T, typename Predicate> qsizetype erase_if(QList<T> &list, Predicate pred)
Removes all elements for which the predicate pred returns true from the list list. Returns the number of elements removed, if any.
This function was introduced in Qt 6.1.
See also erase.
[noexcept(...)]
template <typename T> size_t qHash(const QList<T> &key, size_t seed = 0)
Returns the hash value for key, using seed to seed the calculation.
Type T
must be supported by qHash().
Note: This function is noexcept when noexcept(qHashRange(key.cbegin(), key.cend(), seed))
is true
.
template <typename T> 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 Format of the QDataStream operators.
template <typename T> 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 Format of the QDataStream operators.
© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.