Add QVariantList extraction from a QVariant with a sequential container.
Change-Id: Id9fac7bf47ed3fbb385222b25941215ac24b8b88 Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@digia.com>
This commit is contained in:
parent
01fb843af8
commit
620f788f86
@ -3085,6 +3085,13 @@ QDebug operator<<(QDebug dbg, const QVariant::Type p)
|
||||
returned. Note that this only works for QObject subclasses which use the
|
||||
Q_OBJECT macro.
|
||||
|
||||
If the QVariant contains a sequential container and \c{T} is QVariantList, the
|
||||
elements of the container will be converted into QVariants and returned as a QVariantList.
|
||||
|
||||
\snippet code/src_corelib_kernel_qvariant.cpp 9
|
||||
|
||||
The qRegisterSequentialConverter method must first be called for the container.
|
||||
|
||||
\sa setValue(), fromValue(), canConvert()
|
||||
*/
|
||||
|
||||
|
@ -674,6 +674,22 @@ namespace QtPrivate {
|
||||
return QSequentialIterable(v.value<QtMetaTypePrivate::QSequentialIterableImpl>());
|
||||
}
|
||||
};
|
||||
template<>
|
||||
struct QVariantValueHelperInterface<QVariantList>
|
||||
{
|
||||
static QVariantList invoke(const QVariant &v)
|
||||
{
|
||||
if (v.userType() == qMetaTypeId<QStringList>() || QMetaType::hasRegisteredConverterFunction(v.userType(), qMetaTypeId<QtMetaTypePrivate::QSequentialIterableImpl>())) {
|
||||
QSequentialIterable iter = QVariantValueHelperInterface<QSequentialIterable>::invoke(v);
|
||||
QVariantList l;
|
||||
l.reserve(iter.size());
|
||||
for (QSequentialIterable::const_iterator it = iter.begin(), end = iter.end(); it != end; ++it)
|
||||
l << *it;
|
||||
return l;
|
||||
}
|
||||
return QVariantValueHelper<QVariantList>::invoke(v);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
template<typename T> inline T qvariant_cast(const QVariant &v)
|
||||
|
@ -3364,6 +3364,24 @@ struct ContainerAPI
|
||||
{
|
||||
return variant.value<typename Container::value_type>() == value;
|
||||
}
|
||||
static bool compare(QVariant variant, const QVariant &value)
|
||||
{
|
||||
return variant == value;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Container>
|
||||
struct ContainerAPI<Container, QVariant>
|
||||
{
|
||||
static void insert(Container &container, int value)
|
||||
{
|
||||
container.push_back(QVariant::fromValue(value));
|
||||
}
|
||||
|
||||
static bool compare(QVariant variant, const QVariant &value)
|
||||
{
|
||||
return variant == value;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Container>
|
||||
@ -3378,6 +3396,10 @@ struct ContainerAPI<Container, QString>
|
||||
{
|
||||
return variant.value<QString>() == value;
|
||||
}
|
||||
static bool compare(QVariant variant, const QVariant &value)
|
||||
{
|
||||
return variant == value;
|
||||
}
|
||||
};
|
||||
|
||||
// We have no built-in defines to check the stdlib features.
|
||||
@ -3400,7 +3422,26 @@ struct ContainerAPI<std::forward_list<Value_Type> >
|
||||
{
|
||||
return variant.value<Value_Type>() == value;
|
||||
}
|
||||
static bool compare(QVariant variant, const QVariant &value)
|
||||
{
|
||||
return variant == value;
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ContainerAPI<std::forward_list<QVariant> >
|
||||
{
|
||||
static void insert(std::forward_list<QVariant> &container, int value)
|
||||
{
|
||||
container.push_front(QVariant::fromValue(value));
|
||||
}
|
||||
|
||||
static bool compare(QVariant variant, const QVariant &value)
|
||||
{
|
||||
return variant == value;
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ContainerAPI<std::forward_list<QString> >
|
||||
{
|
||||
@ -3412,6 +3453,10 @@ struct ContainerAPI<std::forward_list<QString> >
|
||||
{
|
||||
return variant.value<QString>() == value;
|
||||
}
|
||||
static bool compare(QVariant variant, const QVariant &value)
|
||||
{
|
||||
return variant == value;
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
@ -3424,6 +3469,7 @@ void tst_QVariant::iterateContainerElements()
|
||||
containerIter = intList.begin(); \
|
||||
for (QVariant v : listIter) { \
|
||||
QVERIFY(ContainerAPI<CONTAINER<VALUE_TYPE > >::compare(v, *containerIter)); \
|
||||
QVERIFY(ContainerAPI<CONTAINER<VALUE_TYPE > >::compare(v, varList.at(numSeen))); \
|
||||
++containerIter; \
|
||||
++numSeen; \
|
||||
} \
|
||||
@ -3445,13 +3491,17 @@ void tst_QVariant::iterateContainerElements()
|
||||
\
|
||||
QVariant listVariant = QVariant::fromValue(intList); \
|
||||
QVERIFY(listVariant.canConvert<QVariantList>()); \
|
||||
QVariantList varList = listVariant.value<QVariantList>(); \
|
||||
QCOMPARE(varList.size(), (int)std::distance(intList.begin(), intList.end())); \
|
||||
QSequentialIterable listIter = listVariant.value<QSequentialIterable>(); \
|
||||
QCOMPARE(varList.size(), listIter.size()); \
|
||||
\
|
||||
CONTAINER<VALUE_TYPE >::iterator containerIter = intList.begin(); \
|
||||
const CONTAINER<VALUE_TYPE >::iterator containerEnd = intList.end(); \
|
||||
for (int i = 0; i < listIter.size(); ++i, ++containerIter, ++numSeen) \
|
||||
{ \
|
||||
QVERIFY(ContainerAPI<CONTAINER<VALUE_TYPE > >::compare(listIter.at(i), *containerIter)); \
|
||||
QVERIFY(ContainerAPI<CONTAINER<VALUE_TYPE > >::compare(listIter.at(i), varList.at(i))); \
|
||||
} \
|
||||
QCOMPARE(numSeen, (int)std::distance(intList.begin(), intList.end())); \
|
||||
QCOMPARE(containerIter, containerEnd); \
|
||||
@ -3460,6 +3510,7 @@ void tst_QVariant::iterateContainerElements()
|
||||
numSeen = 0; \
|
||||
Q_FOREACH (const QVariant &v, listIter) { \
|
||||
QVERIFY(ContainerAPI<CONTAINER<VALUE_TYPE > >::compare(v, *containerIter)); \
|
||||
QVERIFY(ContainerAPI<CONTAINER<VALUE_TYPE > >::compare(v, varList.at(numSeen))); \
|
||||
++containerIter; \
|
||||
++numSeen; \
|
||||
} \
|
||||
|
Loading…
Reference in New Issue
Block a user