diff options
-rw-r--r-- | src/qml/doc/src/qmlfunctions.qdoc | 2 | ||||
-rw-r--r-- | src/qml/qml/qqml.cpp | 11 | ||||
-rw-r--r-- | tests/auto/qml/qqmllanguage/testtypes.h | 7 | ||||
-rw-r--r-- | tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp | 12 |
4 files changed, 29 insertions, 3 deletions
diff --git a/src/qml/doc/src/qmlfunctions.qdoc b/src/qml/doc/src/qmlfunctions.qdoc index 9c8cd5e859..f6ed2306c0 100644 --- a/src/qml/doc/src/qmlfunctions.qdoc +++ b/src/qml/doc/src/qmlfunctions.qdoc @@ -151,6 +151,8 @@ \l QML_NAMED_ELEMENT(). For such types, \l QML_UNCREATABLE() can be used to provide a custom error message. + Since Qt 6.0 you can use "" instead of a reason to use a standard message instead. + \sa QML_ELEMENT, QML_NAMED_ELEMENT(), QML_ANONYMOUS */ diff --git a/src/qml/qml/qqml.cpp b/src/qml/qml/qqml.cpp index 28b48e2eee..a57daf78d2 100644 --- a/src/qml/qml/qqml.cpp +++ b/src/qml/qml/qqml.cpp @@ -202,9 +202,14 @@ int QQmlPrivate::qmlregister(RegistrationType type, void *data) const bool creatable = (elementName != nullptr) && boolClassInfo(type.classInfoMetaObject, "QML.Creatable", true); - const QString noCreateReason = creatable - ? QString() - : QString::fromUtf8(classInfo(type.classInfoMetaObject, "QML.UncreatableReason")); + QString noCreateReason; + + if (!creatable) { + noCreateReason = QString::fromUtf8(classInfo(type.classInfoMetaObject, "QML.UncreatableReason")); + if (noCreateReason.isEmpty()) + noCreateReason = QLatin1String("Type cannot be created in QML."); + } + RegisterType revisionRegistration = { 0, type.typeId, diff --git a/tests/auto/qml/qqmllanguage/testtypes.h b/tests/auto/qml/qqmllanguage/testtypes.h index 8852bf7af9..9c2aaa9e30 100644 --- a/tests/auto/qml/qqmllanguage/testtypes.h +++ b/tests/auto/qml/qqmllanguage/testtypes.h @@ -1527,6 +1527,13 @@ private: UncreatableSingleton() { setObjectName("uncreatable"); } }; +class UncreatableElementNoReason : public QObject +{ + Q_OBJECT + QML_ELEMENT + QML_UNCREATABLE("") +}; + void registerTypes(); #endif // TESTTYPES_H diff --git a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp index 4ea8de5a40..3dbaa2ec4e 100644 --- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp +++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp @@ -333,6 +333,7 @@ private slots: void arrayToContainer(); void qualifiedScopeInCustomParser(); + void checkUncreatableNoReason(); private: QQmlEngine engine; QStringList defaultImportPathList; @@ -5839,6 +5840,17 @@ void tst_qqmllanguage::qualifiedScopeInCustomParser() QVERIFY(!obj.isNull()); } +void tst_qqmllanguage::checkUncreatableNoReason() +{ + qmlRegisterTypesAndRevisions<UncreatableElementNoReason>("qt.uncreatable.noreason", 1, 0); + QQmlEngine engine; + QString qml = QString("import QtQuick 2.0\nimport qt.uncreatable.noreason 1.0\nUncreatableElementNoReason {}"); + QQmlComponent c(&engine); + c.setData(qml.toUtf8(), QUrl::fromLocalFile(QDir::currentPath())); + QCOMPARE(c.errors().count(), 1); + QCOMPARE(c.errors().first().description(), QString("Type cannot be created in QML.")); +} + QTEST_MAIN(tst_qqmllanguage) #include "tst_qqmllanguage.moc" |