QMutableHashIterator Class

template <typename Key, typename T> class QMutableHashIterator

The QMutableHashIterator class provides a Java-style non-const iterator for QHash and QMultiHash. More...

Header: #include <QMutableHashIterator>
qmake: QT += core

Public Functions

QMutableHashIterator(QHash<Key, T> &hash)
QMutableHashIterator<Key, T> &operator=(QHash<Key, T> &container)
bool findNext(const T &value)
bool hasNext() const
const Key &key() const
QMutableHashIterator::Item next()
QMutableHashIterator::Item peekNext() const
void remove()
void setValue(const T &value)
void toBack()
void toFront()
const T &value() const
T &value()

Detailed Description

QHash 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.

QMutableHashIterator<Key, T> allows you to iterate over a QHash (or a QMultiHash) and modify the hash. If you don't want to modify the hash (or have a const QHash), use the slightly faster QHashIterator instead.

The QMutableHashIterator constructor takes a QHash as argument. After construction, the iterator is located at the very beginning of the hash (before the first item). Here's how to iterate over all the elements sequentially:

QHash<int, QWidget *> hash;
...
QMutableHashIterator<QString, QWidget *> i(hash);
while (i.hasNext()) {
    i.next();
    qDebug() << i.key() << ": " << i.value();
}

The next() function returns the next item in the hash and advances the iterator. The key() and value() functions return the key and value of the last item that was jumped over.

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 so on.

If you want to find all occurrences of a particular value, use findNext() in a loop. For example:

QMutableHashIterator<int, QWidget *> i(hash);
while (i.findNext(widget)) {
    qDebug() << "Found widget " << widget << " under key "
             << i.key();
}

If you want to remove items as you iterate over the hash, use remove(). If you want to modify the value of an item, use setValue().

Example:

QMutableHashIterator<QString, QString> i(hash);
while (i.hasNext()) {
    i.next();
    if (i.key() == i.value())
        i.remove();
}

The example removes all (key, value) pairs where the key and the value are the same.

Only one mutable iterator can be active on a given hash at any time. Furthermore, no changes should be done directly to the hash while the iterator is active (as opposed to through the iterator), since this could invalidate the iterator and lead to undefined behavior.

See also QHashIterator and QHash::iterator.

Member Function Documentation

bool QMutableHashIterator::findNext(const T &value)

Searches for value starting from the current iterator position forward. Returns true if a (key, value) pair with value value is found; otherwise returns false.

After the call, if value was found, the iterator is positioned just after the matching item; otherwise, the iterator is positioned at the back of the container.

const Key &QMutableHashIterator::key() const

Returns the key of the last item that was jumped over using one of the traversal functions (next(), findNext()).

See also value().

T &QMutableHashIterator::value()

This is an overloaded function.

Returns a non-const reference to the value of the last item that was jumped over using one of the traversal functions.

bool QMutableHashIterator::hasNext() const

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.

See also next().

void QMutableHashIterator::toBack()

Moves the iterator to the back of the container (after the last item).

See also toFront().

void QMutableHashIterator::toFront()

Moves the iterator to the front of the container (before the first item).

See also toBack() and next().

QMutableHashIterator<Key, T> &QMutableHashIterator::operator=(QHash<Key, T> &container)

Makes the iterator operate on hash. The iterator is set to be at the front of the hash (before the first item).

See also toFront() and toBack().

QMutableHashIterator::QMutableHashIterator(QHash<Key, T> &hash)

Constructs an iterator for traversing hash. The iterator is set to be at the front of the hash (before the first item).

See also operator=().

QMutableHashIterator::Item QMutableHashIterator::next()

Returns the next item and advances the iterator by one position.

Call key() on the return value to obtain the item's key, and value() to obtain the value.

Calling this function on an iterator located at the back of the container leads to undefined results.

See also hasNext() and peekNext().

QMutableHashIterator::Item QMutableHashIterator::peekNext() const

Returns a reference to the next item without moving the iterator.

Call key() on the return value to obtain the item's key, and value() to obtain the value.

Calling this function on an iterator located at the back of the container leads to undefined results.

See also hasNext() and next().

void QMutableHashIterator::remove()

Removes the last item that was jumped over using one of the traversal functions (next(), findNext()).

See also setValue().

void QMutableHashIterator::setValue(const T &value)

Replaces the value of the last item that was jumped over using one of the traversal functions with value.

The traversal functions are next() and findNext().

See also key(), value(), and remove().

const T &QMutableHashIterator::value() const

Returns the value of the last item that was jumped over using one of the traversal functions (next(), findNext()).

See also key() and setValue().

© 2023 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.