QList Class▲
-
Header: QList
-
qmake: QT += core
-
Inherited By: QBluetoothServiceInfo::Alternative, QBluetoothServiceInfo::Sequence, QByteArrayList, QItemSelection, QNdefMessage, QQueue, QSignalSpy, QStringList, and QTestEventList
-
Group: QList is part of tools, Implicitly Shared Classes
Detailed Description▲
QList<T> is one of Qt's generic container classes. It stores items in a list that provides fast index-based access and index-based insertions and removals.
QList<T>, QLinkedList<T>, and QVector<T> provide similar APIs and functionality. They are often interchangeable, but there are performance consequences. Here is an overview of use cases:
-
QVector should be your default first choice. QVector<T> will usually give better performance than QList<T>, because QVector<T> always stores its items sequentially in memory, where QList<T> will allocate its items on the heap unless sizeof(T) <= sizeof(void*) and T has been declared to be either a Q_MOVABLE_TYPE or a Q_PRIMITIVE_TYPE using Q_DECLARE_TYPEINFO. See the Pros and Cons of Using QList for an explanation.
-
However, QList is used throughout the Qt APIs for passing parameters and for returning values. Use QList to interface with those APIs.
-
If you need a real linked list, which guarantees constant time insertions mid-list and uses iterators to items rather than indexes, use QLinkedList.
QVector and QVarLengthArray both guarantee C-compatible array layout. QList does not. This might be important if your application must interface with a C API.
Iterators into a QLinkedList and references into heap-allocating QLists remain valid as long as the referenced items remain in the container. This is not true for iterators and references into a QVector and non-heap-allocating QLists.
Internally, QList<T> is represented as an array of T if sizeof(T) <= sizeof(void*) and T has been declared to be either a Q_MOVABLE_TYPE or a Q_PRIMITIVE_TYPE using Q_DECLARE_TYPEINFO. Otherwise, QList<T> is represented as an array of T* and the items are allocated on the heap.
The array representation allows very fast insertions and index-based access. The prepend() and append() operations are also very fast because QList preallocates memory at both ends of its internal array. (See Algorithmic Complexity for details.
Note, however, that when the conditions specified above are not met, each append or insert of a new item requires allocating the new item on the heap, and this per item allocation will make QVector a better choice for use cases that do a lot of appending or inserting, because QVector can allocate memory for many items in a single heap allocation.
Note that the internal array only ever gets bigger over the life of the list. It never shrinks. The internal array is deallocated by the destructor and by the assignment operator, when one list is assigned to another.
Here's an example of a QList that stores integers and a QList that stores QDate values:
QList&
lt;int
&
gt; integerList;
QList&
lt;QDate&
gt; dateList;
Qt includes a QStringList class that inherits QList<QString> and adds a few convenience functions, such as QStringList::join() and QStringList::filter(). QString::split() creates QStringLists from strings.
QList stores a list of items. The default constructor creates an empty list. You can use the initializer-list constructor to create a list with elements:
QList&
lt;QString&
gt; list =
{
"one"
, "two"
, "three"
}
;
QList provides these basic functions to add, move, and remove items: insert(), replace(), removeAt(), move(), and swap(). In addition, it provides the following convenience functions: append(), operator<<(), operator+=(), prepend(), removeFirst(), and removeLast().
operator<<() allows to conveniently add multiple elements to a list:
list &
lt;&
lt; "four"
&
lt;&
lt; "five"
;
QList uses 0-based indexes, just like C++ arrays. To access the item at a particular index position, you can use operator[](). On non-const lists, operator[]() returns a reference to the item and can be used on the left side of an assignment:
if
(list[0
] ==
"Bob"
)
list[0
] =
"Robert"
;
Because QList is implemented as an array of pointers for types that are larger than a pointer or are not movable, this operation requires (constant time). For read-only access, an alternative syntax is to use at():
for
(int
i =
0
; i &
lt; list.size(); ++
i) {
if
(list.at(i) ==
"Jane"
)
cout &
lt;&
lt; "Found Jane at position "
&
lt;&
lt; i &
lt;&
lt; endl;
}
at() can be faster than operator[](), because it never causes a deep copy to occur.
A common requirement is to remove an item from a list and do something with it. For this, QList provides takeAt(), takeFirst(), and takeLast(). Here's a loop that removes the items from a list one at a time and calls delete on them:
QList&
lt;QWidget *&
gt; list;
...
while
(!
list.isEmpty())
delete
list.takeFirst();
Inserting and removing items at either end of the list is very fast (constant time in most cases), because QList preallocates extra space on both sides of its internal buffer to allow for fast growth at both ends of the list.
If you want to find all occurrences of a particular value in a list, use indexOf() or lastIndexOf(). The former searches forward starting from a given index position, the latter searches backward. Both return the index of a matching item if they find it; otherwise, they return -1. For example:
int
i =
list.indexOf("Jane"
);
if
(i !=
-
1
)
cout &
lt;&
lt; "First occurrence of Jane is at position "
&
lt;&
lt; i &
lt;&
lt; endl;
If you simply want to check whether a list contains a particular value, use contains(). If you want to find out how many times a particular value occurs in the list, use count(). If you want to replace all occurrences of a particular value with another, use replace().
QList's value type must be an assignable data type. This covers most data types that are commonly used, but the compiler won't let you, for example, store a QWidget as a value; instead, store a QWidget *. A few functions have additional requirements; for example, indexOf() and lastIndexOf() expect the value type to support operator==(). These requirements are documented on a per-function basis.
Like the other container classes, QList provides Java-style iterators (QListIterator and QMutableListIterator) and STL-style iterators (QList::const_iterator and QList::iterator). In practice, these are rarely used, because you can use indexes into the QList. QList is implemented in such a way that direct index-based access is just as fast as using iterators.
QList does not support inserting, prepending, appending or replacing with references to its own values. Doing so will cause your application to abort with an error message.
To make QList as efficient as possible, its member functions don't validate their input before using it. Except for isEmpty(), member functions always assume the list is not empty. Member functions that take index values as parameters always assume their index value parameters are in the valid range. This means QList member functions can fail. If you define QT_NO_DEBUG when you compile, failures will not be detected. If you don't define QT_NO_DEBUG, failures will be detected using Q_ASSERT() or Q_ASSERT_X() with an appropriate message.
To avoid failures when your list can be empty, call isEmpty() before calling other member functions. If you must pass an index value that might not be in the valid range, check that it is less than the value returned by size() but not less than 0.
More Members▲
If T is a QByteArray, this class has a couple more members that can be used. See the documentation for QByteArrayList for more information.
If T is QString, this class has the following additional members: filter, join, removeDuplicates, sort.
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.
See Also▲
See also QListIterator, QMutableListIterator, QLinkedList, QVector
Member Type Documentation▲
QList::ConstIterator▲
Qt-style synonym for QList::const_iterator.
QList::Iterator▲
Qt-style synonym for QList::iterator.
QList::const_pointer▲
Typedef for const T *. Provided for STL compatibility.
QList::const_reference▲
Typedef for const T &. Provided for STL compatibility.
[since 5.6] QList::const_reverse_iterator▲
The QList::const_reverse_iterator typedef provides an STL-style const reverse iterator for QList.
It is simply a typedef for std::reverse_iterator<const_iterator>.
Iterators on implicitly shared containers do not work exactly like STL-iterators. You should avoid copying a container while iterators are active on that container. For more information, read Implicit sharing iterator problem.
This typedef was introduced in Qt 5.6.
See Also▲
See also QList::rbegin(), QList::rend(), QList::reverse_iterator, QList::const_iterator
QList::difference_type▲
Typedef for ptrdiff_t. Provided for STL compatibility.
QList::pointer▲
Typedef for T *. Provided for STL compatibility.
QList::reference▲
Typedef for T &. Provided for STL compatibility.
[since 5.6] QList::reverse_iterator▲
The QList::reverse_iterator typedef provides an STL-style non-const reverse iterator for QList.
It is simply a typedef for std::reverse_iterator<iterator>.
Iterators on implicitly shared containers do not work exactly like STL-iterators. You should avoid copying a container while iterators are active on that container. For more information, read Implicit sharing iterator problem.
This typedef was introduced in Qt 5.6.
See Also▲
See also QList::rbegin(), QList::rend(), QList::const_reverse_iterator, QList::iterator
QList::size_type▲
Typedef for int. Provided for STL compatibility.
QList::value_type▲
Typedef for T. Provided for STL compatibility.
Member Function Documentation▲
QList::QList()▲
Constructs an empty list.
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▲
See also operator=()
[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 4.8] QList::QList(std::initializer_list<T> args)▲
Construct a list from the std::initializer_list specified by args.
This constructor is only enabled if the compiler supports C++11 initializer lists.
This function was introduced in Qt 4.8.
QList::~QList()▲
Destroys the list. References to the values in the list and all iterators of this list become invalid.
void QList::append(const T &value)▲
Inserts value at the end of the list.
Example:
QList&
lt;QString&
gt; list;
list.append("one"
);
list.append("two"
);
list.append("three"
);
// list: ["one", "two", "three"]
This is the same as list.insert(size(), value).
If this list is not shared, this operation is typically very fast (amortized constant time), because QList preallocates extra space on both sides of its internal buffer to allow for fast growth at both ends of the list.
See Also▲
See also operator<<(), prepend(), insert()
[since 4.5] void QList::append(const QList<T> &value)▲
This is an overloaded function.
Appends the items of the value list to this list.
This function was introduced in Qt 4.5.
See Also▲
See also operator<<(), operator+=()
const T &QList::at(int i) const▲
Returns the item at index position i in the list. i must be a valid index position in the list (i.e., 0 <= i < size()).
This function is very fast (constant time).
See Also▲
See also value(), operator[]()
T &QList::back()▲
This function is provided for STL compatibility. It is equivalent to last(). The list must not be empty. If the list can be empty, call isEmpty() before calling this function.
const T &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.
See Also▲
See also constBegin(), end()
QList::const_iterator QList::begin() const▲
This is an overloaded function.
[since 5.0] QList::const_iterator QList::cbegin() const▲
Returns a const STL-style iterator pointing to the first item in the list.
This function was introduced in Qt 5.0.
See Also▲
[since 5.0] QList::const_iterator QList::cend() const▲
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list.
This function was introduced in Qt 5.0.
See Also▲
void QList::clear()▲
QList::const_iterator QList::constBegin() const▲
Returns a const STL-style iterator pointing to the first item in the list.
See Also▲
QList::const_iterator QList::constEnd() const▲
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list.
See Also▲
See also constBegin(), end()
[since 5.6] const T &QList::constFirst() const▲
Returns a const reference to the first item in the list. The list must not be empty. If the list can be empty, call isEmpty() before calling this function.
This function was introduced in Qt 5.6.
See Also▲
[since 5.6] const T &QList::constLast() const▲
Returns a reference to the last item in the list. The list must not be empty. If the list can be empty, call isEmpty() before calling this function.
This function was introduced in Qt 5.6.
See Also▲
See also constFirst(), isEmpty(), last()
bool QList::contains(const T &value) const▲
Returns true if the list contains an occurrence of value; otherwise returns false.
This function requires the value type to have an implementation of operator==().
See Also▲
int QList::count(const T &value) const▲
Returns the number of occurrences of value in the list.
This function requires the value type to have an implementation of operator==().
See Also▲
int QList::count() const▲
Returns the number of items in the list. This is effectively the same as size().
[since 5.6] QList::const_reverse_iterator QList::crbegin() const▲
Returns a const STL-style reverse iterator pointing to the first item in the list, in reverse order.
This function was introduced in Qt 5.6.
See Also▲
[since 5.6] QList::const_reverse_iterator QList::crend() const▲
Returns a const STL-style reverse iterator pointing to one past the last item in the list, in reverse order.
This function was introduced in Qt 5.6.
See Also▲
bool QList::empty() const▲
This function is provided for STL compatibility. It is equivalent to isEmpty() and returns true if the list is empty.
QList::iterator QList::end()▲
Returns an STL-style iterator pointing to the imaginary item after the last item in the list.
See Also▲
QList::const_iterator QList::end() const▲
This is an overloaded function.
[since 4.5] bool QList::endsWith(const T &value) const▲
Returns true if this list is not empty and its last item is equal to value; otherwise returns false.
This function was introduced in Qt 4.5.
See Also▲
QList::iterator QList::erase(QList::iterator pos)▲
Removes the item associated with the iterator pos from the list, and returns an iterator to the next item in the list (which may be end()).
See Also▲
QList::iterator QList::erase(QList::iterator begin, QList::iterator end)▲
This is an overloaded function.
Removes all the items from begin up to (but not including) end. Returns an iterator to the same item that end referred to before the call.
T &QList::first()▲
Returns a reference to the first item in the list. The list must not be empty. If the list can be empty, call isEmpty() before calling this function.
See Also▲
See also constFirst(), last(), isEmpty()
const T &QList::first() const▲
This is an overloaded function.
[static] QList<T> QList::fromSet(const QSet<T> &set)▲
Returns a QList object with the data contained in set. The order of the elements in the QList is undefined.
Example:
QSet&
lt;int
&
gt; set;
set &
lt;&
lt; 20
&
lt;&
lt; 30
&
lt;&
lt; 40
&
lt;&
lt; ... &
lt;&
lt; 70
;
QList&
lt;int
&
gt; list =
QList&
lt;int
&
gt;::
fromSet(set);
qSort(list);
See Also▲
See also fromVector(), toSet(), QSet::toList()
[static] QList<T> QList::fromStdList(const std::list<T> &list)▲
Returns a QList object with the data contained in list. The order of the elements in the QList is the same as in list.
Example:
std::
list&
lt;double
&
gt; stdlist;
list.push_back(1.2
);
list.push_back(0.5
);
list.push_back(3.14
);
QList&
lt;double
&
gt; list =
QList&
lt;double
&
gt;::
fromStdList(stdlist);
See Also▲
See also toStdList(), QVector::fromStdVector()
[static] QList<T> QList::fromVector(const QVector<T> &vector)▲
Returns a QList object with the data contained in vector.
Example:
QVector&
lt;double
&
gt; vect;
vect &
lt;&
lt; 20.0
&
lt;&
lt; 30.0
&
lt;&
lt; 40.0
&
lt;&
lt; 50.0
;
QList&
lt;double
&
gt; list =
QVector&
lt;T&
gt;::
fromVector(vect);
// list: [20.0, 30.0, 40.0, 50.0]
See Also▲
See also fromSet(), toVector(), QVector::toList()
T &QList::front()▲
This function is provided for STL compatibility. It is equivalent to first(). The list must not be empty. If the list can be empty, call isEmpty() before calling this function.
const T &QList::front() const▲
This is an overloaded function.
int QList::indexOf(const T &value, int 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&
lt;QString&
gt; list;
list &
lt;&
lt; "A"
&
lt;&
lt; "B"
&
lt;&
lt; "C"
&
lt;&
lt; "B"
&
lt;&
lt; "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==().
Note that QList uses 0-based indexes, just like C++ arrays. Negative indexes are not supported with the exception of the value mentioned above.
See Also▲
See also lastIndexOf(), contains()
void QList::insert(int i, const T &value)▲
Inserts value at index position i in the list. If i <= 0, the value is prepended to the list. If i >= size(), the value is appended to the list.
Example:
QList&
lt;QString&
gt; list;
list &
lt;&
lt; "alpha"
&
lt;&
lt; "beta"
&
lt;&
lt; "delta"
;
list.insert(2
, "gamma"
);
// list: ["alpha", "beta", "gamma", "delta"]
See Also▲
QList::iterator QList::insert(QList::iterator before, const T &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. Note that the iterator passed to the function will be invalid after the call; the returned iterator should be used instead.
bool QList::isEmpty() const▲
T &QList::last()▲
Returns a reference to the last item in the list. The list must not be empty. If the list can be empty, call isEmpty() before calling this function.
See Also▲
const T &QList::last() const▲
This is an overloaded function.
int QList::lastIndexOf(const T &value, int from = -1) const▲
Returns the index position of the last occurrence of value in the list, searching backward from index position from. If from is -1 (the default), the search starts at the last item. Returns -1 if no item matched.
Example:
QList&
lt;QString&
gt; list;
list &
lt;&
lt; "A"
&
lt;&
lt; "B"
&
lt;&
lt; "C"
&
lt;&
lt; "B"
&
lt;&
lt; "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==().
Note that QList uses 0-based indexes, just like C++ arrays. Negative indexes are not supported with the exception of the value mentioned above.
See Also▲
See also indexOf()
[since 4.5] int QList::length() const▲
This function is identical to count().
This function was introduced in Qt 4.5.
See Also▲
See also count()
QList<T> QList::mid(int pos, int length = -1) const▲
Returns a sub-list which includes elements from this list, starting at position pos. If length is -1 (the default), all elements from pos are included; otherwise length elements (or all remaining elements if there are less than length elements) are included.
void QList::move(int from, int to)▲
Moves the item at index position from to index position to.
Example:
QList&
lt;QString&
gt; list;
list &
lt;&
lt; "A"
&
lt;&
lt; "B"
&
lt;&
lt; "C"
&
lt;&
lt; "D"
&
lt;&
lt; "E"
&
lt;&
lt; "F"
;
list.move(1
, 4
);
// list: ["A", "C", "D", "E", "B", "F"]
This is the same as insert(to, takeAt(from)).This function assumes that both from and to are at least 0 but less than size(). To avoid failure, test that both from and to are at least 0 and less than size().
See Also▲
void QList::pop_back()▲
This function is provided for STL compatibility. It is equivalent to removeLast(). The list must not be empty. If the list can be empty, call isEmpty() before calling this function.
void QList::pop_front()▲
This function is provided for STL compatibility. It is equivalent to removeFirst(). The list must not be empty. If the list can be empty, call isEmpty() before calling this function.
void QList::prepend(const T &value)▲
Inserts value at the beginning of the list.
Example:
QList&
lt;QString&
gt; list;
list.prepend("one"
);
list.prepend("two"
);
list.prepend("three"
);
// list: ["three", "two", "one"]
This is the same as list.insert(0, value).
If this list is not shared, this operation is typically very fast (amortized constant time), because QList preallocates extra space on both sides of its internal buffer to allow for fast growth at both ends of the list.
See Also▲
void QList::push_back(const T &value)▲
This function is provided for STL compatibility. It is equivalent to append(value).
void QList::push_front(const T &value)▲
This function is provided for STL compatibility. It is equivalent to prepend(value).
[since 5.6] QList::reverse_iterator QList::rbegin()▲
Returns a STL-style reverse iterator pointing to the first item in the list, in reverse order.
This function was introduced in Qt 5.6.
See Also▲
[since 5.6] QList::const_reverse_iterator QList::rbegin() const▲
This is an overloaded function.
This function was introduced in Qt 5.6.
int QList::removeAll(const T &value)▲
Removes all occurrences of value in the list and returns the number of entries removed.
Example:
QList&
lt;QString&
gt; list;
list &
lt;&
lt; "sun"
&
lt;&
lt; "cloud"
&
lt;&
lt; "sun"
&
lt;&
lt; "rain"
;
list.removeAll("sun"
);
// list: ["cloud", "rain"]
This function requires the value type to have an implementation of operator==().
See Also▲
void QList::removeAt(int i)▲
Removes the item at index position i. i must be a valid index position in the list (i.e., 0 <= i < size()).
See Also▲
See also takeAt(), removeFirst(), removeLast(), removeOne()
void QList::removeFirst()▲
Removes the first item in the list. Calling this function is equivalent to calling removeAt(0). The list must not be empty. If the list can be empty, call isEmpty() before calling this function.
See Also▲
void QList::removeLast()▲
Removes the last item in the list. Calling this function is equivalent to calling removeAt(size() - 1). The list must not be empty. If the list can be empty, call isEmpty() before calling this function.
See Also▲
[since 4.4] bool QList::removeOne(const T &value)▲
Removes the first occurrence of value in the list and returns true on success; otherwise returns false.
Example:
QList&
lt;QString&
gt; list;
list &
lt;&
lt; "sun"
&
lt;&
lt; "cloud"
&
lt;&
lt; "sun"
&
lt;&
lt; "rain"
;
list.removeOne("sun"
);
// list: ["cloud", "sun", "rain"]
This function requires the value type to have an implementation of operator==().
This function was introduced in Qt 4.4.
See Also▲
[since 5.6] QList::reverse_iterator QList::rend()▲
Returns a STL-style reverse iterator pointing to one past the last item in the list, in reverse order.
This function was introduced in Qt 5.6.
See Also▲
[since 5.6] QList::const_reverse_iterator QList::rend() const▲
This is an overloaded function.
This function was introduced in Qt 5.6.
void QList::replace(int i, const T &value)▲
Replaces the item at index position i with value. i must be a valid index position in the list (i.e., 0 <= i < size()).
See Also▲
See also operator[](), removeAt()
[since 4.7] void QList::reserve(int alloc)▲
Reserve space for alloc elements.
If alloc is smaller than the current size of the list, nothing will happen.
Use this function to avoid repetetive reallocation of QList's internal data if you can predict how many elements will be appended. Note that the reservation applies only to the internal pointer array.
This function was introduced in Qt 4.7.
int QList::size() const▲
[since 4.5] bool QList::startsWith(const T &value) const▲
Returns true if this list is not empty and its first item is equal to value; otherwise returns false.
This function was introduced in Qt 4.5.
See Also▲
[since 4.8] void QList::swap(QList<T> &other)▲
Swaps list other with this list. This operation is very fast and never fails.
This function was introduced in Qt 4.8.
void QList::swap(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().
Example:
QList&
lt;QString&
gt; list;
list &
lt;&
lt; "A"
&
lt;&
lt; "B"
&
lt;&
lt; "C"
&
lt;&
lt; "D"
&
lt;&
lt; "E"
&
lt;&
lt; "F"
;
list.swap(1
, 4
);
// list: ["A", "E", "C", "D", "B", "F"]
See Also▲
See also move()
T QList::takeAt(int i)▲
Removes the item at index position i and returns it. i must be a valid index position in the list (i.e., 0 <= i < size()).
If you don't use the return value, removeAt() is more efficient.
See Also▲
T QList::takeFirst()▲
Removes the first item in the list and returns it. This is the same as takeAt(0). This function assumes the list is not empty. To avoid failure, call isEmpty() before calling this function.
If this list is not shared, this operation takes constant time.
If you don't use the return value, removeFirst() is more efficient.
See Also▲
See also takeLast(), takeAt(), removeFirst()
T QList::takeLast()▲
Removes the last item in the list and returns it. This is the same as takeAt(size() - 1). This function assumes the list is not empty. To avoid failure, call isEmpty() before calling this function.
If this list is not shared, this operation takes constant time.
If you don't use the return value, removeLast() is more efficient.
See Also▲
See also takeFirst(), takeAt(), removeLast()
QSet<T> QList::toSet() const▲
Returns a QSet object with the data contained in this QList. Since QSet doesn't allow duplicates, the resulting QSet might be smaller than the original list was.
Example:
QStringList list;
list &
lt;&
lt; "Julia"
&
lt;&
lt; "Mike"
&
lt;&
lt; "Mike"
&
lt;&
lt; "Julia"
&
lt;&
lt; "Julia"
;
QSet&
lt;QString&
gt; set =
list.toSet();
set.contains("Julia"
); // returns true
set.contains("Mike"
); // returns true
set.size(); // returns 2
See Also▲
See also toVector(), fromSet(), QSet::fromList()
std::list<T> QList::toStdList() const▲
Returns a std::list object with the data contained in this QList. Example:
QList&
lt;double
&
gt; list;
list &
lt;&
lt; 1.2
&
lt;&
lt; 0.5
&
lt;&
lt; 3.14
;
std::
list&
lt;double
&
gt; stdlist =
list.toStdList();
See Also▲
See also fromStdList(), QVector::toStdVector()
QVector<T> QList::toVector() const▲
Returns a QVector object with the data contained in this QList.
Example:
QStringList list;
list &
lt;&
lt; "Sven"
&
lt;&
lt; "Kim"
&
lt;&
lt; "Ola"
;
QVector&
lt;QString&
gt; vect =
list.toVector();
// vect: ["Sven", "Kim", "Ola"]
See Also▲
See also toSet(), fromVector(), QVector::fromList()
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 the index is going to be within bounds, you can use at() instead, which is slightly faster.
See Also▲
See also at(), operator[]()
T QList::value(int i, const T &defaultValue) const▲
This is an overloaded function.
If the index i is out of bounds, the function returns defaultValue.
bool QList::operator!=(const QList<T> &other) const▲
Returns true if other is not equal to this list; otherwise returns false.
Two lists are considered equal if they contain the same values in the same order.
This function requires the value type to have an implementation of operator==().
See Also▲
See also operator==()
QList<T> QList::operator+(const QList<T> &other) const▲
Returns a list that contains all the items in this list followed by all the items in the other list.
See Also▲
See also 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▲
QList<T> &QList::operator+=(const T &value)▲
This is an overloaded function.
Appends value to the list.
See Also▲
See also append(), 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▲
See also operator+=(), append()
QList<T> &QList::operator<<(const T &value)▲
This is an overloaded function.
Appends value to the list.
QList<T> &QList::operator=(const QList<T> &other)▲
Assigns other to this list and returns a reference to this list.
[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.
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▲
See also operator!=()
T &QList::operator[](int i)▲
Returns the item at index position i as a modifiable reference. i must be a valid index position in the list (i.e., 0 <= i < size()).
If this function is called on a list that is currently being shared, it will trigger a copy of all elements. Otherwise, this function runs in constant time. If you do not want to modify the list you should use QList::at().
See Also▲
const T &QList::operator[](int i) const▲
This is an overloaded function.
Same as at(). This function runs in constant time.
Related Non-Members▲
[since 5.6] uint qHash(const QList<T> &key, uint seed = 0)▲
Returns the hash value for key, using seed to seed the calculation.
This function requires qHash() to be overloaded for the value type T.
This function was introduced in Qt 5.6.
[since 5.6] bool operator<(const QList<T> &lhs, const QList<T> &rhs)▲
Returns true if list lhs is lexicographically less than rhs; otherwise returns false.
This function requires the value type to have an implementation of operator<().
This function was introduced in Qt 5.6.
QDataStream &operator<<(QDataStream &out, const QList<T> &list)▲
Writes the list list to stream out.
This function requires the value type to implement operator<<().
See Also▲
See also Format of the QDataStream operators
[since 5.6] bool operator<=(const QList<T> &lhs, const QList<T> &rhs)▲
Returns true if list lhs is lexicographically less than or equal to rhs; 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 operator>(const QList<T> &lhs, const QList<T> &rhs)▲
Returns true if list lhs is lexicographically greater than rhs; 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 operator>=(const QList<T> &lhs, const QList<T> &rhs)▲
Returns true if list lhs is lexicographically greater than or equal to rhs; otherwise returns false.
This function requires the value type to have an implementation of operator<().
This function was introduced in Qt 5.6.
QDataStream &operator>>(QDataStream &in, QList<T> &list)▲
Reads a list from stream in into list.
This function requires the value type to implement operator>>().
See Also▲
See also Format of the QDataStream operators