From e35b82922398e09779a162592cdedd50677e63cf Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Tue, 5 Jan 2021 17:52:00 +0100 Subject: QQmlListReference: Allow construction from a QVariant Currently there is no public API to transform a QVariant holding a QQmlListProperty into a QQmlListReference. We cannot pass QQmlListProperty itself as that is templated. The metatype-based casting is somewhat evil, but not more so than what we already have in the other ctor. Change-Id: I2d56499b1fd188613bc71016cb00ec23081d3cea Reviewed-by: Fabian Kosmale Reviewed-by: Andrei Golubev --- .../qqmllistreference/tst_qqmllistreference.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'tests') diff --git a/tests/auto/qml/qqmllistreference/tst_qqmllistreference.cpp b/tests/auto/qml/qqmllistreference/tst_qqmllistreference.cpp index bf5ee92374..c6accb3908 100644 --- a/tests/auto/qml/qqmllistreference/tst_qqmllistreference.cpp +++ b/tests/auto/qml/qqmllistreference/tst_qqmllistreference.cpp @@ -179,6 +179,17 @@ void tst_qqmllistreference::qmllistreference() tt.data.append(&tt); QCOMPARE(r.count(), 1); + + const QMetaObject *m = tt.metaObject(); + const int index = m->indexOfProperty("data"); + const QMetaProperty prop = m->property(index); + const QVariant var = prop.read(&tt); + + QQmlListReference fromVar(var); + QVERIFY(fromVar.isValid()); + QCOMPARE(fromVar.count(), 1); + fromVar.append(&tt); + QCOMPARE(tt.data.count(), 2); } void tst_qqmllistreference::qmllistreference_invalid() @@ -805,6 +816,17 @@ void tst_qqmllistreference::engineTypes() QVERIFY(ref.listElementType()); QVERIFY(ref.listElementType() != &QObject::staticMetaObject); + + const QMetaObject *m = o->metaObject(); + const int index = m->indexOfProperty("myList"); + const QMetaProperty prop = m->property(index); + const QVariant var = prop.read(o); + + QQmlListReference fromVar(var, &engine); + QVERIFY(fromVar.isValid()); + QCOMPARE(fromVar.count(), 2); + QCOMPARE(fromVar.listElementType(), ref.listElementType()); + delete o; } -- cgit v1.2.3