Qt5 Tutorial QListIterator - 2020
In this tutorial, we will learn about QListIterator.
QList has both Java-style iterators and STL-style iterators. The Java-style iterators are more high-level and easier to use than the STL-style iterators; on the other hand, they are slightly less efficient.
The QListIterator class provides a Java-style const iterator for QList.
QListIterator<T> allows us to iterate over a QList<T>. If we want to modify the list as we iterate over it, use QMutableListIterator<T> instead.
#include <QCoreApplication> #include <QList> #include <QDebug> #include <QListIterator> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QList<int> List; for(int i = 0; i < 10; i++) List.append(i); // The QListIterator constructor takes a QList // as argument. After construction, the iterator // is located at the very beginning of the list // (before the first item) QListIterator<int> iter(List); qDebug() << "Forward..."; while(iter.hasNext()) { // The next() function returns the next item // in the list and advances the iterator. qDebug() << iter.next(); } qDebug() << "Backward..."; while(iter.hasPrevious()) { // The next() function returns the next item // in the list and advances the iterator. qDebug() << iter.previous(); } return a.exec(); }
Output:
Forward... 0 1 2 3 4 5 6 7 8 9 Backward... 9 8 7 6 5 4 3 2 1 0
Note that the QListIterator constructor takes a QList as argument. After construction, the iterator is located at the very beginning of the list (before the first item).
The hasNext() returns true if there is at least one item ahead of the iterator, i.e. the iterator is not at the back of the container; otherwise returns false.
The next() function returns the next item in the list and advances the iterator. Unlike STL-style iterators, Java-style iterators point between items rather than directly at items. The first call to next() advances the iterator to the position between the first and second item, and returns the first item; the second call to next() advances the iterator to the position between the second and third item, and returns the second item; and so on.
The hasPrevious() returns true if there is at least one item behind the iterator, i.e. the iterator is not at the front of the container; otherwise returns false.
The previous() returns the previous item and moves the iterator back by one position. Calling this function on an iterator located at the front of the container leads to undefined results.
We can peek before we iterate:
#include <QCoreApplication> #include <QList> #include <QDebug> #include <QListIterator> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QList<int> List; for(int i = 0; i < 10; i++) List.append(i); // The QListIterator constructor takes a QList // as argument. After construction, the iterator // is located at the very beginning of the list // (before the first item) QListIterator<int> iter(List); qDebug() << "Forward..."; while(iter.hasNext()) { // The next() function returns the next item // in the list and advances the iterator. qDebug() << iter.next(); } qDebug() << "Backward..."; while(iter.hasPrevious()) { // The next() function returns the next item // in the list and advances the iterator. qDebug() << iter.previous(); } qDebug() << "Forward while peeking..."; while(iter.hasNext()) { qDebug() << "peek next ..." << iter.peekNext(); qDebug() << iter.next(); } return a.exec(); }
Output:
... Forware while peeking... peek next ... 0 0 peek next ... 1 1 peek next ... 2 2 peek next ... 3 3 peek next ... 4 4 peek next ... 5 5 peek next ... 6 6 peek next ... 7 7 peek next ... 8 8 peek next ... 9 9
The peekNext() Returns the next item without moving the iterator. Calling this function on an iterator located at the back of the container leads to undefined results.
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization