6.0.0-rc4Source

QListIterator Class

template <typename T, typename C = QList<T>> class QListIterator

The QListIterator class provides a Java-style const iterator for QList. More…

Header#include <QListIterator>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Functions

QListIterator(const C &container)
QListIterator &operator=(const C &container)
voidtoFront()
voidtoBack()
boolhasNext() const
boolhasPrevious() const
const T &next()
const T &previous()
const T &peekNext() const
const T &peekPrevious() const
boolfindNext(const T &v)
boolfindPrevious(const T &v)

Detailed Description

QListIterator<T> iterates over a list without modifying it. The iterator points between items, not at them: next() returns the item it jumps over and moves forward, and previous() moves back and returns the item it jumps over. A new iterator is positioned before the first item.

QList<QString> list = {"A", "B", "C"};
QListIterator<QString> it(list);
while (it.hasNext())
    qDebug() << it.next();

The iterator refers to the container it was created from, which must not be destroyed or modified while the iterator is in use. QVectorIterator is an alias of this class.

See also QMutableListIterator and QSetIterator.

Differences from Qt

The second template argument selects the container type and defaults to QList<T>. An iterator is assigned from a container, not from another iterator.

Member Function Documentation

[explicit] QListIterator::QListIterator(const C &container)

Constructs an iterator for traversing container. The iterator is set to be at the front of the list, before the first item.

See also toFront().

bool QListIterator::findNext(const T &v)

Searches for v forward from the iterator position. Returns true if an item equal to v is found, leaving the iterator just after it; otherwise returns false and leaves the iterator at the back of the list.

See also findPrevious().

bool QListIterator::findPrevious(const T &v)

Searches for v backward from the iterator position. Returns true if an item equal to v is found, leaving the iterator just before it; otherwise returns false and leaves the iterator at the front of the list.

See also findNext().

bool QListIterator::hasNext() const

Returns true if there is at least one item ahead of the iterator; otherwise returns false.

See also hasPrevious() and next().

bool QListIterator::hasPrevious() const

Returns true if there is at least one item behind the iterator; otherwise returns false.

See also hasNext() and previous().

const T & QListIterator::next()

Returns the next item and advances the iterator by one position. Calling this function when the iterator is at the back of the list is undefined.

See also hasNext(), peekNext() and previous().

QListIterator & QListIterator::operator=(const C &container)

Makes the iterator operate on container and moves it to the front of the list. Returns a reference to this iterator.

const T & QListIterator::peekNext() const

Returns the next item without moving the iterator. Calling this function when the iterator is at the back of the list is undefined.

See also hasNext(), next() and peekPrevious().

const T & QListIterator::peekPrevious() const

Returns the previous item without moving the iterator. Calling this function when the iterator is at the front of the list is undefined.

See also hasPrevious(), previous() and peekNext().

const T & QListIterator::previous()

Returns the previous item and moves the iterator back by one position. Calling this function when the iterator is at the front of the list is undefined.

See also hasPrevious(), peekPrevious() and next().

void QListIterator::toBack()

Moves the iterator to the back of the list, after the last item.

See also toFront() and previous().

void QListIterator::toFront()

Moves the iterator to the front of the list, before the first item.

See also toBack() and next().