From 05a9fa44378a96838d56b37eaa59223919d5a721 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Wed, 21 Feb 2024 15:01:17 +0100 Subject: QtQml: Load C++-defined types also for the implicit import If the implicit import does not declare a plugin, that doesn't mean we don't want to load its C++-defined types. Pick-to: 6.7 Change-Id: I23427a91f7801cab3a18a5b01c45c520fae7e271 Reviewed-by: Fabian Kosmale Reviewed-by: Qt CI Bot --- src/qml/qml/qqmlimport.cpp | 7 ++- tests/auto/qml/qqmlimport/data/noimport/Main.qml | 5 ++ tests/auto/qml/qqmlimport/data/noimport/qmldir | 3 ++ tests/auto/qml/qqmlimport/tst_qqmlimport.cpp | 65 +++++++++++++++++++----- 4 files changed, 65 insertions(+), 15 deletions(-) create mode 100644 tests/auto/qml/qqmlimport/data/noimport/Main.qml create mode 100644 tests/auto/qml/qqmlimport/data/noimport/qmldir diff --git a/src/qml/qml/qqmlimport.cpp b/src/qml/qml/qqmlimport.cpp index 8a2d102e41..99e31fcfe8 100644 --- a/src/qml/qml/qqmlimport.cpp +++ b/src/qml/qml/qqmlimport.cpp @@ -921,8 +921,13 @@ QTypeRevision QQmlImports::importExtension( return QTypeRevision(); } - if (qmldir->plugins().isEmpty()) + if (qmldir->plugins().isEmpty()) { + // If the qmldir does not register a plugin, we might still have declaratively + // registered types (if we are dealing with an application instead of a library) + if (!QQmlMetaType::typeModule(uri, version)) + QQmlMetaType::qmlRegisterModuleTypes(uri); return validVersion(version); + } QQmlPluginImporter importer( uri, version, typeLoader->importDatabase(), qmldir, typeLoader, errors); diff --git a/tests/auto/qml/qqmlimport/data/noimport/Main.qml b/tests/auto/qml/qqmlimport/data/noimport/Main.qml new file mode 100644 index 0000000000..d473b43d7b --- /dev/null +++ b/tests/auto/qml/qqmlimport/data/noimport/Main.qml @@ -0,0 +1,5 @@ +pragma Strict + +TheThing { + width: 640 +} diff --git a/tests/auto/qml/qqmlimport/data/noimport/qmldir b/tests/auto/qml/qqmlimport/data/noimport/qmldir new file mode 100644 index 0000000000..3034edba51 --- /dev/null +++ b/tests/auto/qml/qqmlimport/data/noimport/qmldir @@ -0,0 +1,3 @@ +module noimport +Main 1.0 Main.qml + diff --git a/tests/auto/qml/qqmlimport/tst_qqmlimport.cpp b/tests/auto/qml/qqmlimport/tst_qqmlimport.cpp index f08a8d423a..37a90a74b4 100644 --- a/tests/auto/qml/qqmlimport/tst_qqmlimport.cpp +++ b/tests/auto/qml/qqmlimport/tst_qqmlimport.cpp @@ -1,23 +1,49 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 -#include -#include -#include -#include +#include +#include +#include + #include #include -#include -#include -#include -#include + +#include + +#include +#include +#include +#include + +#include +#include + +class TheThing : public QObject +{ + Q_OBJECT + QML_ELEMENT + Q_PROPERTY(int width MEMBER m_width FINAL) + +public: + int m_width = 12; +}; + +void qml_register_types_noimport() +{ + qmlRegisterTypesAndRevisions("noimport", 1); + qmlRegisterModule("noimport", 1, 0); +} class tst_QQmlImport : public QQmlDataTest { Q_OBJECT public: - tst_QQmlImport(); + tst_QQmlImport() + : QQmlDataTest(QT_QMLTEST_DATADIR) + , noimportRegistration("noimport", qml_register_types_noimport) + { + } private slots: void importPathOrder(); @@ -40,6 +66,10 @@ private slots: void implicitWithDependencies(); void qualifiedScriptImport(); void invalidImportUrl(); + void registerTypesFromImplicitImport(); + +private: + QQmlModuleRegistration noimportRegistration; }; void tst_QQmlImport::cleanup() @@ -150,6 +180,18 @@ void tst_QQmlImport::invalidImportUrl() ":2 Cannot resolve URL for import \"file://./MyModuleName\"\n")); } +void tst_QQmlImport::registerTypesFromImplicitImport() +{ + QQmlEngine engine; + QQmlComponent c(&engine, testFileUrl("noimport/Main.qml")); + QVERIFY2(c.isReady(), qPrintable(c.errorString())); + QScopedPointer o(c.create()); + QVERIFY(!o.isNull()); + TheThing *t = qobject_cast(o.data()); + QVERIFY(t); + QCOMPARE(t->m_width, 640); +} + void tst_QQmlImport::testDesignerSupported() { std::unique_ptr window = std::make_unique(); @@ -219,11 +261,6 @@ void tst_QQmlImport::uiFormatLoading() QVERIFY(test->rootObjects()[size -1]->property("success").toBool()); } -tst_QQmlImport::tst_QQmlImport() - : QQmlDataTest(QT_QMLTEST_DATADIR) -{ -} - void tst_QQmlImport::importPathOrder() { #ifdef Q_OS_ANDROID -- cgit v1.2.3