On this page

QMetaAssociation::Iterable::ConstIterator Class

class QMetaAssociation::Iterable::ConstIterator

QMetaAssociation::Iterable::ConstIterator allows iteration over a container in a QVariant. More...

This class was introduced in Qt 6.11.

Public Functions

QVariant key() const
QVariant value() const
QVariant operator*() const
int operator->() const

Detailed Description

A QMetaAssociation::Iterable::ConstIterator can only be created by a QMetaAssociation::Iterable instance, and can be used in a way similar to other stl-style iterators.

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>()) {
    QMetaAssociation::Iterable iterable = variant.value<QMetaAssociation::Iterable>();
    // Can use C++11 range-for over the values:
    for (const QVariant &v : iterable) {
        qDebug() << v;
    }
    // Can use iterators:
    QMetaAssociation::Iterable::const_iterator it = iterable.begin();
    const QMetaAssociation::Iterable::const_iterator end = iterable.end();
    for ( ; it != end; ++it) {
        qDebug() << *it; // The current value
        qDebug() << it.key();
        qDebug() << it.value();
    }
}

See also QMetaAssociation::Iterable.

Member Function Documentation

QVariant ConstIterator::key() const

Returns the key this iterator points to.

QVariant ConstIterator::value() const

Returns the mapped value this iterator points to, or an invalid QVariant if there is no mapped value.

QVariant ConstIterator::operator*() const

Returns the current item, converted to a QVariant. The returned value is the mapped value at the current iterator if there is one, or otherwise the key.

int ConstIterator::operator->() const

Returns the current item, converted to a QVariant::ConstPointer. The QVariant::ConstPointer will resolve to the mapped value at the current iterator if there is one, or otherwise the key.

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