diff options
author | Stephen Kelly <stephen.kelly@kdab.com> | 2013-03-18 21:53:55 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-05-08 06:02:45 +0200 |
commit | 01fb843af88d949cd38b494a60bb64b730a045d2 (patch) | |
tree | e5f1f8b6b38f971cd8112f0ed51a7a8a782ad429 /src/corelib/doc | |
parent | f7b313e6d865ac7020c3164c2f6f0232f052eb9b (diff) |
Add QVariant container iteration API.
A new set of classes is introduced for iterating over the contents
of a container within a QVariant without knowing the exact type of
the container, but with the guarantee that the element type within
the container is a metatype.
The implementation of the iterable interface uses
the stl-compatible container API so that we can also iterate over stl
containers, or any other container which also conforms to stl norms.
This enables the functionality in the bug report.
Task-number: QTBUG-23566
Change-Id: I92a2f3458516de201b8f0e470982c4d030e8ac8b
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
Diffstat (limited to 'src/corelib/doc')
-rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_kernel_qvariant.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_kernel_qvariant.cpp b/src/corelib/doc/snippets/code/src_corelib_kernel_qvariant.cpp index 25d24185ee..27971d6f60 100644 --- a/src/corelib/doc/snippets/code/src_corelib_kernel_qvariant.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_kernel_qvariant.cpp @@ -134,3 +134,33 @@ return QVariant::fromValue(s); QObject *object = getObjectFromSomewhere(); QVariant data = QVariant::fromValue(object); //! [8] + +//! [9] + +qRegisterSequentialConverter<QList<int> >(); + +QList<int> intList; +intList.push_back(7); +intList.push_back(11); +intList.push_back(42); + +QVariant variant = QVariant::fromValue(intList); +if (variant.canConvert<QVariantList>()) { + QSequentialIterable iterable = variant.value<QSequentialIterable>(); + // Can use foreach: + foreach (const QVariant &v, iterable) { + qDebug() << v; + } + // Can use C++11 range-for: + for (const QVariant &v : iterable) { + qDebug() << v; + } + // Can use iterators: + QSequentialIterable::const_iterator it = iterable.begin(); + const QSequentialIterable::const_iterator end = iterable.end(); + for ( ; it != end; ++it) { + qDebug() << *it; + } +} + +//! [9] |