Java-style Iterators in Qt¶
Java style iterators for Qt’s containers.
For each container class, there are two Java-style iterator data types: one that provides read-only access and one that provides read-write access.
In this discussion, we will concentrate on
QMap . The iterator types for
QSet have exactly the same interface as
QList ‘s iterators; similarly, the iterator types for
QHash have the same interface as
QMap ‘s iterators.
Unlike STL-Style iterators , Java-style iterators point between items rather than directly at items. For this reason, they are either pointing to the very beginning of the container (before the first item), at the very end of the container (after the last item), or between two items. The diagram below shows the valid iterator positions as red arrows for a list containing four items:
Here’s a typical loop for iterating through all the elements of a
QString > in order and printing them to the console:
list = QList() list << "A" << "B" << "C" << "D" i = QListIterator(list) while i.hasNext(): print(i.next())
It works as follows: The
QList to iterate over is passed to the
QListIterator constructor. At that point, the iterator is located just in front of the first item in the list (before item “A”). Then we call
hasNext() to check whether there is an item after the iterator. If there is, we call
next() to jump over that item. The next() function returns the item that it jumps over. For a
QString >, that item is of type
Here’s how to iterate backward in a
i = QListIterator(list) i.toBack() while i.hasPrevious(): print(i.previous())
The code is symmetric with iterating forward, except that we start by calling
toBack() to move the iterator after the last item in the list.
The diagram below illustrates the effect of calling
previous() on an iterator:
The following table summarizes the
Moves the iterator to the front of the list (before the first item)
Moves the iterator to the back of the list (after the last item)
trueif the iterator isn’t at the back of the list
Returns the next item and advances the iterator by one position
Returns the next item without moving the iterator
trueif the iterator isn’t at the front of the list
Returns the previous item and moves the iterator back by one position
Returns the previous item without moving the iterator
QListIterator provides no functions to insert or remove items from the list as we iterate. To accomplish this, you must use
QMutableListIterator . Here’s an example where we remove all odd numbers from a
QList <int> using
i = QMutableListIterator(list) while i.hasNext(): if (i.next() % 2 != 0) i.remove()
The next() call in the loop is made every time. It jumps over the next item in the list. The
remove() function removes the last item that we jumped over from the list. The call to
remove() does not invalidate the iterator, so it is safe to continue using it. This works just as well when iterating backward:
i = QMutableListIterator(list) i.toBack() while i.hasPrevious(): if (i.previous() % 2 != 0) i.remove()
If we just want to modify the value of an existing item, we can use
setValue() . In the code below, we replace any value larger than 128 with 128:
i = QMutableListIterator(list) while i.hasNext(): if (i.next() > 128) i.setValue(128)
setValue() operates on the last item that we jumped over. If we iterate forward, this is the item just before the iterator; if we iterate backward, this is the item just after the iterator.
next() function returns a non-const reference to the item in the list. For simple operations, we don’t even need
i = QMutableListIterator(list) while i.hasNext(): i.next() *= 2
As mentioned above
QSet ‘s iterator classes have exactly the same API as
QList ‘s. We will now turn to
QMapIterator , which is somewhat different because it iterates on (key, value) pairs.
previous() , and
peekPrevious() . The key and value components are extracted by calling
value() on the object returned by next(), peekNext(), previous(), or peekPrevious().
The following example removes all (capital, country) pairs where the capital’s name ends with “City”:
QString> = QMap<QString,() map.insert("Paris", "France") map.insert("Guatemala City", "Guatemala") map.insert("Mexico City", "Mexico") map.insert("Moscow", "Russia") ... QString> = QMutableMapIterator<QString,(map) while i.hasNext(): if (i.next().key().endsWith("City")) i.remove()
QMapIterator also provides a
key() and a
value() function that operate directly on the iterator and that return the key and value of the last item that the iterator jumped above. For example, the following code copies the contents of a
QMap into a
QWidget = QMap<int,() QWidget = QHash<int,() QWidget = QMapIterator<int,(map) while i.hasNext(): i.next() hash.insert(i.key(), i.value())
If we want to iterate through all the items with the same value, we can use
findPrevious() . Here’s an example where we remove all the items with a particular value:
QWidget = QMutableMapIterator<int,(map) while i.findNext(widget): i.remove()
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.