QLinkedList Class

template <typename T> class QLinkedList

The QLinkedList class is a template class that provides linked lists. More...

Header: #include <QLinkedList>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core5Compat)
target_link_libraries(mytarget PRIVATE Qt6::Core5Compat)
qmake: QT += core5compat

Note: All functions in this class are reentrant.

Public Types

Detailed Description

QLinkedList<T> is one of Qt's generic container classes. It stores a list of values and provides iterator-based access as well as constant time insertions and removals.

QList<T> and QLinkedList<T> provide similar functionality. Here's an overview:

  • For most purposes, QList is the right class to use. Its index-based API is more convenient than QLinkedList's iterator-based API. Its items occupy adjacent memory positions. It also expands to less code in your executable.
  • If you need a real linked list, with guarantees of constant time insertions in the middle of the list and iterators to items rather than indexes, use QLinkedList.

Here's an example of a QLinkedList that stores integers and a QLinkedList that stores QTime values:

 QLinkedList<int> integerList;
 QLinkedList<QTime> timeList;

QLinkedList stores a list of items. The default constructor creates an empty list. To insert items into the list, you can use operator<<():

 QLinkedList<QString> list;
 list << "one" << "two" << "three";
 // list: ["one", "two", "three"]

If you want to get the first or last item in a linked list, use first() or last(). If you want to remove an item from either end of the list, use removeFirst() or removeLast(). If you want to remove all occurrences of a given value in the list, use removeAll().

A common requirement is to remove the first or last item in the list and do something with it. For this, QLinkedList provides takeFirst() and takeLast(). Here's a loop that removes the items from a list one at a time and calls delete on them:

 QLinkedList<QWidget *> list;
 ...
 while (!list.isEmpty())
     delete list.takeFirst();

QLinkedList'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, contains() and removeAll() expect the value type to support operator==(). These requirements are documented on a per-function basis.

If you want to insert, modify, or remove items in the middle of the list, you must use an iterator. QLinkedList provides both Java-style iterators (QLinkedListIterator and QMutableLinkedListIterator) and STL-style iterators (QLinkedList::const_iterator and QLinkedList::iterator). See the documentation for these classes for details.

See also QLinkedListIterator, QMutableLinkedListIterator, and QList.

Member Type Documentation

QLinkedList::ConstIterator

Qt-style synonym for QLinkedList::const_iterator.

QLinkedList::Iterator

Qt-style synonym for QLinkedList::iterator.

QLinkedList::const_pointer

Typedef for const T *. Provided for STL compatibility.

QLinkedList::const_reference

Typedef for const T &. Provided for STL compatibility.

QLinkedList::const_reverse_iterator

The QLinkedList::const_reverse_iterator typedef provides an STL-style const reverse iterator for QLinkedList.

It is simply a typedef for std::reverse_iterator<QLinkedList::const_iterator>.

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.

See also QLinkedList::rbegin(), QLinkedList::rend(), QLinkedList::reverse_iterator, and QLinkedList::const_iterator.

QLinkedList::difference_type

Typedef for ptrdiff_t. Provided for STL compatibility.

QLinkedList::pointer

Typedef for T *. Provided for STL compatibility.

QLinkedList::reference

Typedef for T &. Provided for STL compatibility.

QLinkedList::reverse_iterator

The QLinkedList::reverse_iterator typedef provides an STL-style non-const reverse iterator for QLinkedList.

It is simply a typedef for std::reverse_iterator<QLinkedList::iterator>.

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.

See also QLinkedList::rbegin(), QLinkedList::rend(), QLinkedList::const_reverse_iterator, and QLinkedList::iterator.

QLinkedList::size_type

Typedef for int. Provided for STL compatibility.

QLinkedList::value_type

Typedef for T. Provided for STL compatibility.