QAssociativeIterable 类

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

头: #include <QAssociativeIterable>
qmake: QT += core
Since: Qt 5.2

公共类型

class const_iterator

公共函数

const_iterator begin () const
const_iterator end () const
const_iterator find (const QVariant & key ) const
int size () const
QVariant value (const QVariant & key ) const

详细描述

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

此类允许一些方法访问关联容器的元素,保持在 QVariant . An instance of QAssociativeIterable can be extracted from a QVariant 若它可以被转换成 QVariantHash or QVariantMap .

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();
    }
}
					

容器本身不会被拷贝,在遍历它之前。

另请参阅 QVariant .

成员函数文档编制

const_iterator QAssociativeIterable:: begin () const

返回 QAssociativeIterable::const_iterator for the beginning of the container. This can be used in stl-style iteration.

另请参阅 end ().

const_iterator QAssociativeIterable:: end () const

返回 QAssociativeIterable::const_iterator for the end of the container. This can be used in stl-style iteration.

另请参阅 begin ().

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

返回 QAssociativeIterable::const_iterator for the given key key in the container, if the types are convertible.

若找不到键,返回 end ().

This can be used in stl-style iteration.

该函数在 Qt 5.5 引入。

另请参阅 begin (), end (),和 value ().

int QAssociativeIterable:: size () const

Returns the number of elements in the container.

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

Returns the value for the given key in the container, if the types are convertible.

另请参阅 find ().