From 50ba4ffdf48461d38f4dd2137a0fbbc916c9eb0a Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Mon, 2 Nov 2020 12:15:40 +0100 Subject: QML: Allow singleton types to be extended It seems we never stated that singletons can not be extended in our documentation. Therefore this is technically a bug fix and doesn't need separate documentation. Change-Id: I7877289bd5a52ecf709f80ba1975137981ec65f0 Reviewed-by: Fabian Kosmale --- tests/auto/qml/qqmllanguage/testtypes.h | 24 ++++++++++++++++++++++++ tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp | 23 +++++++++++++++++++++++ 2 files changed, 47 insertions(+) (limited to 'tests/auto/qml') diff --git a/tests/auto/qml/qqmllanguage/testtypes.h b/tests/auto/qml/qqmllanguage/testtypes.h index cdcc5faed8..003e9d06ad 100644 --- a/tests/auto/qml/qqmllanguage/testtypes.h +++ b/tests/auto/qml/qqmllanguage/testtypes.h @@ -1583,6 +1583,30 @@ private: FactorySingleton() = default; }; +class ExtendedSingleton : public QObject { + Q_OBJECT + QML_ELEMENT + QML_SINGLETON + QML_EXTENDED(Extension) + + Q_PROPERTY(int foo READ foo CONSTANT) +public: + + int foo() const { return 315; } +}; + +class NamespaceExtendedSingleton : public QObject { + Q_OBJECT + QML_ELEMENT + QML_SINGLETON + QML_EXTENDED_NAMESPACE(ExtensionNamespace) + + Q_PROPERTY(int foo READ foo CONSTANT) +public: + + int foo() const { return 316; } +}; + void registerTypes(); #endif // TESTTYPES_H diff --git a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp index 40a2ee887a..9a9f13ee0a 100644 --- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp +++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp @@ -342,6 +342,7 @@ private slots: void registerValueTypes(); void extendedNamespace(); void factorySingleton(); + void extendedSingleton(); private: QQmlEngine engine; @@ -6065,6 +6066,28 @@ void tst_qqmllanguage::factorySingleton() QCOMPARE(obj->property("mine").toInt(), 314); } +void tst_qqmllanguage::extendedSingleton() +{ + QQmlEngine engine; + QQmlComponent c(&engine); + c.setData("import StaticTest\n" + "import QtQml\n" + "QtObject {\n" + " property int a: ExtendedSingleton.foo\n" + " property int b: NamespaceExtendedSingleton.foo\n" + " property int c: ExtendedSingleton.extension\n" + " property int d: NamespaceExtendedSingleton.Bar\n" + "}", QUrl()); + QVERIFY2(c.isReady(), qPrintable(c.errorString())); + QScopedPointer obj(c.create()); + QVERIFY(!obj.isNull()); + + QCOMPARE(obj->property("a").toInt(), 315); + QCOMPARE(obj->property("b").toInt(), 316); + QCOMPARE(obj->property("c").toInt(), 42); + QCOMPARE(obj->property("d").toInt(), 9); +} + QTEST_MAIN(tst_qqmllanguage) #include "tst_qqmllanguage.moc" -- cgit v1.2.3