IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

QAssociativeIterable Class

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

This class was introduced in Qt 5.2.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

QAssociativeIterable Class

  • Header: QAssociativeIterable

  • Since: Qt 5.2

  • qmake: QT += core

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.

 
Sélectionnez
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

See also QVariant

Member Function Documentation

 

QAssociativeIterable::const_iterator QAssociativeIterable::begin() const

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

See Also

See also end()

QAssociativeIterable::const_iterator QAssociativeIterable::end() const

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

See Also

See also begin()

[since 5.5] QAssociativeIterable::const_iterator QAssociativeIterable::find(const QVariant &key) const

Returns a QAssociativeIterable::const_iterator for the given key key in the container, if the types are convertible.

If the key is not found, returns end().

This can be used in stl-style iteration.

This function was introduced in Qt 5.5.

See Also

See also 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.

See Also

See also find()

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+