diff options
author | Richard Weickelt <richard@weickelt.de> | 2018-05-31 21:41:47 +0200 |
---|---|---|
committer | Richard Weickelt <richard@weickelt.de> | 2018-06-21 18:00:11 +0000 |
commit | 73fdeb9c1c70079e54104c93811b5d7ff9e4ee0b (patch) | |
tree | c6c170c7a27fa8fa1715c035515c3ba849c097c9 /tests/auto/qml/qqmllanguage | |
parent | f44782d0cdbdb800d9c31d5aff712fbf29d52edc (diff) |
Provide API to access singletons associated with a QQmlEngine
This patch adds allows C++ code to retrieve the instance of a registered
singleton type. Until now this required a deturn via QML expression.
Two methods are added to QQmlEngine: A generic one that encapsulates all
singleton objects in a QJSValue and a template function for QObject-derived
singleton types.
An additional convenience function is added to query the QML type id. This
function may also be used for other purposes in the future.
[ChangeLog][QtQml][QQmlEngine] Added API to access singletons associated with
a QQmlEngine.
Task-number: QTBUG-39970
Change-Id: I67c132ede35f80b9aaf1c5e5456715cf4f1b0848
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'tests/auto/qml/qqmllanguage')
-rw-r--r-- | tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp index 39f973f3fd..f3569c2efe 100644 --- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp +++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp @@ -290,6 +290,8 @@ private slots: void valueTypeGroupPropertiesInBehavior(); + void retrieveQmlTypeId(); + private: QQmlEngine engine; QStringList defaultImportPathList; @@ -4956,6 +4958,26 @@ void tst_qqmllanguage::valueTypeGroupPropertiesInBehavior() QCOMPARE(animation->property("easing").value<QEasingCurve>().type(), QEasingCurve::InOutQuad); } +void tst_qqmllanguage::retrieveQmlTypeId() +{ + // Register in reverse order to provoke wrong minor version matching. + int id2 = qmlRegisterType<QObject>("Test", 2, 3, "SomeTestType"); + int id1 = qmlRegisterType<QObject>("Test", 2, 1, "SomeTestType"); + QCOMPARE(qmlTypeId("Test", 2, 1, "SomeTestType"), id1); + QCOMPARE(qmlTypeId("Test", 2, 2, "SomeTestType"), id1); + QCOMPARE(qmlTypeId("Test", 2, 3, "SomeTestType"), id2); + + // Error cases + QCOMPARE(qmlTypeId("Test", 2, 0, "SomeTestType"), -1); + QCOMPARE(qmlTypeId("Test", 2, 3, "DoesNotExist"), -1); + QCOMPARE(qmlTypeId("DoesNotExist", 2, 3, "SomeTestType"), -1); + + // Must also work for other types (defined in testtpes.cpp) + QVERIFY(qmlTypeId("Test", 1, 0, "MyExtendedUncreateableBaseClass") >= 0); + QVERIFY(qmlTypeId("Test", 1, 0, "MyUncreateableBaseClass") >= 0); + QVERIFY(qmlTypeId("Test", 1, 0, "MyTypeObjectSingleton") >= 0); +} + QTEST_MAIN(tst_qqmllanguage) #include "tst_qqmllanguage.moc" |