本页内容

QMetaAssociation::Iterable::ConstIterator Class

class QMetaAssociation::Iterable::ConstIterator

QMetaAssociation::Iterable::ConstIterator 允许在QVariant 中对容器进行迭代。更多内容...

该类在 Qt 6.11 中引入。

公共函数

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

详细说明

QMetaAssociation::Iterable::ConstIterator 只能由QMetaAssociation::Iterable 实例创建,其使用方式与其他STL风格的迭代器类似。

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>();
    // 可以对这些值使用 C++11 的范围 for 循环:
    for(constQVariant&v: iterable) {
        qDebug() << v;
    }
    // 可以使用迭代器:
    QMetaAssociation::Iterable::const_iteratorit=iterable.begin();
    constQMetaAssociation::Iterable::const_iteratorend=iterable.end();
    for( ; it!=end;++it) {
        qDebug() << *it; // The current value
        qDebug() << it.key();
        qDebug() << it.value();
    }
}

另请参阅 QMetaAssociation::Iterable 。

成员函数文档

QVariant ConstIterator::key() const

返回该迭代器所指向的键。

QVariant ConstIterator::value() const

返回该迭代器所指向的映射值;如果没有映射值,则返回一个无效的QVariant 。

QVariant ConstIterator::operator*() const

返回当前项,并将其转换为QVariant 。返回值是当前迭代器处的映射值(如果存在),否则返回键。

int ConstIterator::operator->() const

返回当前项,将其转换为QVariant::ConstPointer 。如果存在映射值,则QVariant::ConstPointer 将解析为当前迭代器处的映射值;否则,将解析为该键。

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