From 01fb843af88d949cd38b494a60bb64b730a045d2 Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Mon, 18 Mar 2013 21:53:55 +0100 Subject: 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 --- .../snippets/code/src_corelib_kernel_qvariant.cpp | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/corelib/doc/snippets/code/src_corelib_kernel_qvariant.cpp') 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 intList; +intList.push_back(7); +intList.push_back(11); +intList.push_back(42); + +QVariant variant = QVariant::fromValue(intList); +if (variant.canConvert()) { + QSequentialIterable iterable = variant.value(); + // 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] -- cgit v1.2.3