QList Class

template <typename T> class QList

The 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:

QBluetoothServiceInfo::Alternative, QQueue, QStack, QVector, and QVulkanInfoVector

Note: All functions in this class are reentrant.

Public Types

Public Functions

QList(const QList<T> &other)
QList(QList<T> &&other)
QList(InputIterator first, InputIterator last)
QList(int size, QList::parameter_type value)
QList(int args)
QList()
QList<T> &operator=(QList<T> &&other)
QList<T> &operator=(const QList<T> &other)
~QList()
void append(QList::parameter_type value)
void append(QList::rvalue_ref value)
void append(const QList<T> &value)
void append(QList<T> &&value)
QList::const_reference at(int i) const
QList::reference back()
QList::const_reference back() const
QList::iterator begin()
QList::const_iterator begin() const
int capacity() const
QList::const_iterator cbegin() const
QList::const_iterator cend() const
void clear()
QList::const_iterator constBegin() const
QList::const_pointer constData() const
QList::const_iterator constEnd() const
const T &constFirst() const
const T &constLast() const
bool contains(const AT &value) const
int count(const AT &) const
int count() const
int crbegin() const
int crend() const
QList::pointer data()
QList::const_pointer data() const
QList::iterator emplace(int i, Args &&... args)
QList::iterator emplace(QList::const_iterator before, Args &&... args)
QList::reference emplaceBack(Args &&... args)
QList::reference emplace_back(Args &&... args)
bool empty() const
QList::iterator end()
QList::const_iterator end() const
bool endsWith(QList::parameter_type value) const
QList::iterator erase(QList::const_iterator pos)
QList::iterator erase(QList::const_iterator begin, QList::const_iterator end)
QList<T> &fill(QList::parameter_type value, int size = -1)
T &first()
const T &first() const
QList<T> first(int n) const
QList::reference front()
QList::const_reference front() const
int indexOf(const AT &, int) const
QList::iterator insert(int i, QList::parameter_type value)
QList::iterator insert(int i, int count, QList::parameter_type value)
QList::iterator insert(QList::const_iterator before, QList::parameter_type value)
QList::iterator insert(QList::const_iterator before, int count, QList::parameter_type value)
QList::iterator insert(QList::const_iterator before, QList::rvalue_ref value)
QList::iterator insert(int i, QList::rvalue_ref value)
bool isEmpty() const
T &last()
const T &last() const
QList<T> last(int n) const
int lastIndexOf(const AT &, int) const
int length() const
QList<T> mid(int pos, int length = -1) const
void move(int from, int to)
void pop_back()
void pop_front()
void prepend(QList::rvalue_ref value)
void prepend(QList::parameter_type value)
void push_back(QList::parameter_type value)
void push_back(QList::rvalue_ref value)
void push_front(QList::rvalue_ref value)
void push_front(QList::parameter_type value)
int rbegin()
int rbegin() const
void remove(int i, int n = 1)
int removeAll(const AT &)
void removeAt(int i)
void removeFirst()
int removeIf(Predicate)
void removeLast()
bool removeOne(const AT &t)
int rend()
int rend() const
void replace(int i, QList::parameter_type value)
void replace(int i, QList::rvalue_ref value)
void reserve(int size)
void resize(int size)
void shrink_to_fit()
int size() const
QList<T> sliced(int pos, int n) const
QList<T> sliced(int pos) const
void squeeze()
bool startsWith(QList::parameter_type value) const
void swapItemsAt(int i, int j)
T takeAt(int i)
QList::value_type takeFirst()
QList::value_type takeLast()
T value(int i) const
T value(int i, QList::parameter_type defaultValue) const
bool operator!=(const QList<T> &other) const
QList<T> operator+(const QList<T> &other) const
QList<T> operator+(QList<T> &&other) const
QList<T> &operator+=(const QList<T> &other)
QList<T> &operator+=(QList<T> &&other)
QList<T> &operator+=(QList::parameter_type value)
QList<T> &operator+=(QList::rvalue_ref value)
bool operator<(const QList<T> &other) const
QList<T> &operator<<(QList::parameter_type value)
QList<T> &operator<<(const QList<T> &other)
QList<T> &operator<<(QList<T> &&other)
QList<T> &operator<<(QList::rvalue_ref value)
bool operator<=(const QList<T> &other) const
QList<T> &operator=(int args)
bool operator>(const QList<T> &other) const
bool operator>=(const QList<T> &other) const
QList::reference operator[](int i)
QList::const_reference operator[](int i) const
int erase(QList<T> &, const AT &)
int erase_if(QList<T> &, Predicate)
QDataStream &operator<<(QDataStream &out, const 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<int> integerList;
 QList<QString> stringList;

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:

 QList<QString> list(200);

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:

 QList<QString> list(200, "Pass");

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 << Qt::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 << Qt::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.

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, iterators are handy when working with generic algorithms provided by Qt and the C++ standard library. Java-style iterators are provided for backwards compatibility, prefer STL-style iterators when writing C++ code.

Note: Iterators over a QList, and references to individual elements within one, cannot be relied on to remain valid when any non-const method of the QList is called. Accessing such an iterator or reference after the call to a non-const method leads to undefined behavior. When stability for iterator-like functionality is required, you should use indexes instead of iterators as they are not tied to QList's internal state and thus do not get invalidated.

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::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::rvalue_ref

[alias] QList::value_type

Provided for STL compatibility.

Member Function Documentation

void QList::push_front(QList::parameter_type value)

void QList::push_front(QList::rvalue_ref value)

This function is provided for STL compatibility. It is equivalent to prepend(value).

void QList::replace(int i, QList::parameter_type value)

void QList::replace(int i, QList::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().

QList::iterator QList::insert(QList::const_iterator before, QList::parameter_type value)

QList::iterator QList::insert(QList::const_iterator before, QList::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.

QList::iterator QList::insert(int i, QList::parameter_type value)

QList::iterator QList::insert(int i, QList::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;
 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().

template <typename Args> QList::reference QList::emplaceBack(Args &&... args)

template <typename Args> QList::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:

 QList<QString> list;
 auto &ref = list.emplaceBack();
 ref = "one";
 // list: ["one"]

This is the same as list.emplace(list.size(), args).

See also emplace.

void QList::prepend(QList::parameter_type value)

void QList::prepend(QList::rvalue_ref 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).

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

[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, since 5.2] QList::QList(QList<T> &&other)

Move-constructs a QList instance, making it point at the same object that other was pointing to.

This function was introduced in Qt 5.2.

[since 5.14] template <typename InputIterator, int> QList::QList(InputIterator first, InputIterator last)

Constructs a list with the contents in the iterator range [first, last).

The value type of InputIterator must be convertible to T.

This function was introduced in Qt 5.14.

QList::QList(int size, QList::parameter_type value)

Constructs a list with an initial size of size elements. Each element is initialized with value.

See also resize() and fill().

QList::QList(int args)

Constructs a list from the std::initializer_list given by args.

QList::QList()

Constructs an empty list.

See also resize().

[default, since 5.2] QList<T> &QList::operator=(QList<T> &&other)

Move-assigns other to this QList instance.

This function was introduced in Qt 5.2.

[default] QList<T> &QList::operator=(const QList<T> &other)

Assigns other to this list and returns a reference to this list.

[default] QList::~QList()

Destroys the list.

void QList::append(QList::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 5.6] void QList::append(QList::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: ""

This function was introduced in Qt 5.6.

[since 5.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 5.5.

See also operator<<() and operator+=().

[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+=().

QList::const_reference 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 < size()).

See also value() and operator[]().

QList::reference QList::back()

This function is provided for STL compatibility. It is equivalent to last().

QList::const_reference QList::back() const

This is an overloaded function.

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

QList::const_iterator QList::begin() const

This is an overloaded function.

int 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().

[since 5.0] QList::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.

This function was introduced in Qt 5.0.

See also begin() and cend().

[since 5.0] QList::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.

This function was introduced in Qt 5.0.

See also cbegin() and end().

void QList::clear()

Removes all the elements from the list.

Note: Until Qt 5.6, this also released the memory used by the list. From Qt 5.7, the capacity is preserved. To shed all capacity, swap with a default-constructed list:

 QList<T> l ...;
 QList<T>().swap(l);
 Q_ASSERT(l.capacity() == 0);

or call squeeze().

See also squeeze().

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

QList::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[]().

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

[since 5.6] 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.

This function was introduced in Qt 5.6.

See also constLast(), isEmpty(), and first().

[since 5.6] 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.

This function was introduced in Qt 5.6.

See also constFirst(), isEmpty(), and last().

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

template <typename AT> int QList::count(const AT &) 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().

int QList::count() const

This is an overloaded function.

Same as size().

[since 5.6] int 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.

This function was introduced in Qt 5.6.

See also begin(), rbegin(), and rend().

[since 5.6] int 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.

This function was introduced in Qt 5.6.

See also end(), rend(), and rbegin().

QList::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[]().

QList::const_pointer QList::data() const

This is an overloaded function.

template <typename Args> QList::iterator QList::emplace(int 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:

 QList<QString> list{"a", "ccc"};
 list.emplace(1, 2, 'b');
 // list: ["a", "bb", "ccc"]

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::iterator QList::emplace(QList::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.

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

QList::const_iterator QList::end() const

This is an overloaded function.

bool QList::endsWith(QList::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::iterator QList::erase(QList::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::iterator QList::erase(QList::const_iterator begin, QList::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::parameter_type value, int 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().

const T &QList::first() const

This is an overloaded function.

[since 6.0] QList<T> QList::first(int 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.

See also last() and sliced().

QList::reference QList::front()

This function is provided for STL compatibility. It is equivalent to first().

QList::const_reference QList::front() const

This is an overloaded function.

template <typename AT> int QList::indexOf(const AT &, int) 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;
 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::iterator QList::insert(int i, int count, QList::parameter_type value)

This is an overloaded function.

Inserts count copies of value at index position i in the list.

Example:

 QList<double> list;
 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]

QList::iterator QList::insert(QList::const_iterator before, int count, QList::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.

bool QList::isEmpty() const

Returns true if the list has size 0; otherwise returns false.

See also size() and resize().

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

const T &QList::last() const

This is an overloaded function.

[since 6.0] QList<T> QList::last(int 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().

template <typename AT> int QList::lastIndexOf(const AT &, int) 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;
 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().

[since 5.2] int QList::length() const

Same as size() and count().

This function was introduced in Qt 5.2.

See also size() and count().

QList<T> QList::mid(int pos, int 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.

[since 5.6] void QList::move(int from, int to)

Moves the item at index position from to index position to.

This function was introduced in Qt 5.6.

void QList::pop_back()

This function is provided for STL compatibility. It is equivalent to removeLast().

void QList::pop_front()

This function is provided for STL compatibility. It is equivalent to removeFirst().

void QList::push_back(QList::parameter_type value)

This function is provided for STL compatibility. It is equivalent to append(value).

[since 5.6] void QList::push_back(QList::rvalue_ref value)

This is an overloaded function.

This function was introduced in Qt 5.6.

[since 5.6] int 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.

This function was introduced in Qt 5.6.

See also begin(), crbegin(), and rend().

[since 5.6] int QList::rbegin() const

This is an overloaded function.

This function was introduced in Qt 5.6.

void QList::remove(int i, int 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().

[since 5.4] template <typename AT> int QList::removeAll(const AT &)

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

This function was introduced in Qt 5.4.

See also removeOne().

[since 5.2] void QList::removeAt(int 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.

This function was introduced in Qt 5.2.

See also remove().

[since 5.1] 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().

This function was introduced in Qt 5.1.

See also remove(), takeFirst(), and isEmpty().

[since 6.1] template <typename Predicate> int QList::removeIf(Predicate)

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

[since 5.1] 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().

This function was introduced in Qt 5.1.

See also remove(), takeLast(), removeFirst(), and isEmpty().

[since 5.4] template <typename AT> 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().

This function was introduced in Qt 5.4.

See also removeAll().

[since 5.6] int 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.

This function was introduced in Qt 5.6.

See also end(), crend(), and rbegin().

[since 5.6] int QList::rend() const

This is an overloaded function.

This function was introduced in Qt 5.6.

void QList::reserve(int 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().

void QList::resize(int size)

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 a default-constructed value. If size is less than the current size, elements are removed from the end.

Since Qt 5.6, resize() doesn't shrink the capacity anymore. To shed excess capacity, use squeeze().

See also size().

[since 5.10] void QList::shrink_to_fit()

This function is provided for STL compatibility. It is equivalent to squeeze().

This function was introduced in Qt 5.10.

int QList::size() const

Returns the number of items in the list.

See also isEmpty() and resize().

[since 6.0] QList<T> QList::sliced(int pos, int 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.

See also first() and last().

[since 6.0] QList<T> QList::sliced(int 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.

See also first() and last().

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

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 size(). To avoid failure, test that both i and j are at least 0 and less than size().

[since 5.2] T QList::takeAt(int 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.

This function was introduced in Qt 5.2.

See also takeFirst() and takeLast().

[since 5.1] QList::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.

This function was introduced in Qt 5.1.

See also takeLast() and removeFirst().

[since 5.1] QList::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.

This function was introduced in Qt 5.1.

See also takeFirst() and removeLast().

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 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(int i, QList::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) const

Returns a list that contains all the items in this list followed by all the items in the other list.

See also operator+=().

[since 6.0] QList<T> QList::operator+(QList<T> &&other) const

This is an overloaded function.

This function was introduced in Qt 6.0.

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::parameter_type value)

This is an overloaded function.

Appends value to the list.

See also append() and operator<<().

[since 5.11] QList<T> &QList::operator+=(QList::rvalue_ref value)

This is an overloaded function.

This function was introduced in Qt 5.11.

See also append() and operator<<().

[since 5.6] 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<().

This function was introduced in Qt 5.6.

QList<T> &QList::operator<<(QList::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.

[since 5.11] QList<T> &QList::operator<<(QList::rvalue_ref value)

This is an overloaded function.

This function was introduced in Qt 5.11.

See also append() and operator+=().

[since 5.6] 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<().

This function was introduced in Qt 5.6.

[since 5.14] QList<T> &QList::operator=(int args)

Assigns the collection of values in args to this QList instance.

This function was introduced in Qt 5.14.

[since 5.6] 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<().

This function was introduced in Qt 5.6.

[since 5.6] 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<().

This function was introduced in Qt 5.6.

QList::reference 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 < size()).

Note that using non-const operators can cause QList to do a deep copy.

See also at() and value().

QList::const_reference QList::operator[](int i) const

This is an overloaded function.

Same as at(i).

Related Non-Members

[since 6.1] template <typename T, typename AT> int erase(QList<T> &, const AT &)

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> int erase_if(QList<T> &, Predicate)

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.

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.