QAssociativeIterable Class

The QAssociativeIterable class is an iterable interface for an associative container in a QVariant. More...

Header: #include <QAssociativeIterable>
CMake: find_package(Qt6 COMPONENT Core)
target_link_libraries(mytarget PUBLIC Qt::Core)
qmake: QT += core
Since: Qt 5.2
Inherits: QIterable

Public Types

Public Functions

QAssociativeIterable::const_iterator find(const QVariant &key) const
QAssociativeIterable::iterator mutableFind(const QVariant &key)
QVariant value(const QVariant &key) const

Detailed Description

This class allows several methods of accessing the elements of an associative container held within a QVariant. An instance of QAssociativeIterable can be extracted from a QVariant if it can be converted to a QVariantHash or QVariantMap or if a custom mutable view has been registered.

 QHash<int, QString> mapping;
 mapping.insert(7, "Seven");
 mapping.insert(11, "Eleven");
 mapping.insert(42, "Forty-two");

 QVariant variant = QVariant::fromValue(mapping);
 if (variant.canConvert<QVariantHash>()) {
     QAssociativeIterable iterable = variant.value<QAssociativeIterable>();
     // Can use foreach over the values:
     foreach (const QVariant &v, iterable) {
         qDebug() << v;
     }
     // Can use C++11 range-for over the values:
     for (const QVariant &v : iterable) {
         qDebug() << v;
     }
     // Can use iterators:
     QAssociativeIterable::const_iterator it = iterable.begin();
     const QAssociativeIterable::const_iterator end = iterable.end();
     for ( ; it != end; ++it) {
         qDebug() << *it; // The current value
         qDebug() << it.key();
         qDebug() << it.value();
     }
 }

The container itself is not copied before iterating over it.

See also QVariant.

Member Type Documentation

[alias] QAssociativeIterable::BidirectionalConstIterator

This is a type alias for QTaggedIterator<const_iterator, std::bidirectional_iterator_tag>.

Exposes a const_iterator using std::bidirectional_iterator_tag.

[alias] QAssociativeIterable::BidirectionalIterator

This is a type alias for QTaggedIterator<iterator, std::bidirectional_iterator_tag>.

Exposes an iterator using std::bidirectional_iterator_tag.

[alias] QAssociativeIterable::ForwardConstIterator

This is a type alias for QTaggedIterator<const_iterator, std::forward_iterator_tag>.

Exposes a const_iterator using std::forward_iterator_tag.

[alias] QAssociativeIterable::ForwardIterator

This is a type alias for QTaggedIterator<iterator, std::forward_iterator_tag>.

Exposes an iterator using std::forward_iterator_tag.

[alias] QAssociativeIterable::InputConstIterator

This is a type alias for QTaggedIterator<const_iterator, std::input_iterator_tag>.

Exposes a const_iterator using std::input_iterator_tag.

[alias] QAssociativeIterable::InputIterator

This is a type alias for QTaggedIterator<iterator, std::input_iterator_tag>.

Exposes an iterator using std::input_iterator_tag.

[alias] QAssociativeIterable::RandomAccessConstIterator

This is a type alias for QTaggedIterator<const_iterator, std::random_access_iterator_tag>.

Exposes a const_iterator using std::random_access_iterator_tag.

[alias] QAssociativeIterable::RandomAccessIterator

This is a type alias for QTaggedIterator<iterator, std::random_access_iterator_tag>.

Exposes an iterator using std::random_access_iterator_tag.

Member Function Documentation

QAssociativeIterable::const_iterator QAssociativeIterable::find(const QVariant &key) const

Retrieves a const_iterator pointing to the element at the given key, or the end of the container if that key does not exist.

QAssociativeIterable::iterator QAssociativeIterable::mutableFind(const QVariant &key)

Retrieves an iterator pointing to the element at the given key, or the end of the container if that key does not exist.

QVariant QAssociativeIterable::value(const QVariant &key) const

Retrieves the mapped value at the given key, or an invalid QVariant if the key does not exist.