diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2021-08-18 09:54:06 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-08-19 12:42:05 +0000 |
commit | dccccaf53b8da87ee4fb59d688017e65a10eb983 (patch) | |
tree | 8e5028189e421d1f4658fb8e5e11788067a53660 | |
parent | 240da0de09228da1925b1bdafe7b67987d3013eb (diff) |
Allow import of modules with only C++ types built into the binary
Check if the module provides a .qmltypes file. If so, we assume that
there are some C++ types to be imported.
Change-Id: Iad15d9c834c9bcebd76a62926620902031493557
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit 358976b7ad28226136d3ac04260f82e88dbf784a)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/qml/qml/qqmlimport.cpp | 8 | ||||
-rw-r--r-- | src/qml/qml/qqmltypeloaderqmldircontent_p.h | 1 | ||||
-rw-r--r-- | tests/auto/cmake/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/auto/cmake/empty_qmldir/CMakeLists.txt | 30 | ||||
-rw-r--r-- | tests/auto/cmake/empty_qmldir/enemy.cpp | 33 | ||||
-rw-r--r-- | tests/auto/cmake/empty_qmldir/enemy.h | 47 | ||||
-rw-r--r-- | tests/auto/cmake/empty_qmldir/flee.qml | 7 | ||||
-rw-r--r-- | tests/auto/cmake/empty_qmldir/main.cpp | 56 |
8 files changed, 181 insertions, 2 deletions
diff --git a/src/qml/qml/qqmlimport.cpp b/src/qml/qml/qqmlimport.cpp index a5c11fa2f2..7088d25192 100644 --- a/src/qml/qml/qqmlimport.cpp +++ b/src/qml/qml/qqmlimport.cpp @@ -1369,8 +1369,12 @@ QTypeRevision QQmlImportsPrivate::addLibraryImport( return matchingVersion; if (inserted->qmlDirComponents.isEmpty() && inserted->qmlDirScripts.isEmpty()) { - if (qmldir.plugins().isEmpty() && !qmldir.imports().isEmpty()) - return validVersion(); // This is a pure redirection + if (qmldir.plugins().isEmpty()) { + if (!qmldir.imports().isEmpty()) + return validVersion(); // This is a pure redirection + if (qmldir.hasTypeInfo()) + return validVersion(); // A pure C++ module without plugin + } errors->prepend(moduleNotFoundError(uri, relevantVersion(uri, version))); return QTypeRevision(); } else if (qmldir.hasContent()) { diff --git a/src/qml/qml/qqmltypeloaderqmldircontent_p.h b/src/qml/qml/qqmltypeloaderqmldircontent_p.h index 55cd846925..41755767ba 100644 --- a/src/qml/qml/qqmltypeloaderqmldircontent_p.h +++ b/src/qml/qml/qqmltypeloaderqmldircontent_p.h @@ -84,6 +84,7 @@ public: QString preferredPath() const { return m_parser.preferredPath(); } bool designerSupported() const { return m_parser.designerSupported(); } + bool hasTypeInfo() const { return !m_parser.typeInfos().isEmpty(); } private: QQmlDirParser m_parser; diff --git a/tests/auto/cmake/CMakeLists.txt b/tests/auto/cmake/CMakeLists.txt index 1be8dbdf31..c1bad1abbc 100644 --- a/tests/auto/cmake/CMakeLists.txt +++ b/tests/auto/cmake/CMakeLists.txt @@ -62,6 +62,7 @@ if(TARGET Qt::Qml) if(NOT QT6_IS_SHARED_LIBS_BUILD) _qt_internal_test_expect_pass(test_plugins) endif() + _qt_internal_test_expect_pass(empty_qmldir) endif() if(TARGET Qt::Quick) diff --git a/tests/auto/cmake/empty_qmldir/CMakeLists.txt b/tests/auto/cmake/empty_qmldir/CMakeLists.txt new file mode 100644 index 0000000000..432e6e99da --- /dev/null +++ b/tests/auto/cmake/empty_qmldir/CMakeLists.txt @@ -0,0 +1,30 @@ +cmake_minimum_required(VERSION 3.16) +project(empty_qmldir_test) + +find_package(Qt6 ${CMAKE_Core_MODULE_MAJOR_VERSION}.${CMAKE_Core_MODULE_MINOR_VERSION} + REQUIRED COMPONENTS Qml Gui Test +) + +set(CMAKE_AUTOMOC ON) + +# Simplify finding the backing targets' DLLs on Windows +if(WIN32) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) +endif() + +qt_add_executable(empty_qmldir_test main.cpp) +target_link_libraries(empty_qmldir_test PRIVATE Qt6::Test) + +qt_add_qml_module(empty_qmldir_test + URI Third + VERSION 1.0 + NO_RESOURCE_TARGET_PATH + SOURCES + enemy.cpp + enemy.h + QML_FILES + flee.qml +) + +# Ensure linting runs when building the default "all" target +set_target_properties(all_qmllint PROPERTIES EXCLUDE_FROM_ALL FALSE) diff --git a/tests/auto/cmake/empty_qmldir/enemy.cpp b/tests/auto/cmake/empty_qmldir/enemy.cpp new file mode 100644 index 0000000000..31df68de6d --- /dev/null +++ b/tests/auto/cmake/empty_qmldir/enemy.cpp @@ -0,0 +1,33 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "enemy.h" + +Enemy::Enemy(QObject *parent) : QObject(parent) { } + +#include "moc_enemy.cpp" diff --git a/tests/auto/cmake/empty_qmldir/enemy.h b/tests/auto/cmake/empty_qmldir/enemy.h new file mode 100644 index 0000000000..ea42220c7c --- /dev/null +++ b/tests/auto/cmake/empty_qmldir/enemy.h @@ -0,0 +1,47 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef ENEMY_H +#define ENEMY_H + +#include <QtQml/qqml.h> + +class Enemy : public QObject +{ + Q_OBJECT + QML_NAMED_ELEMENT(Enemy) + Q_PROPERTY(QString name MEMBER name BINDABLE bindableName) + +public: + Enemy(QObject *parent = nullptr); + + QProperty<QString> name; + QBindable<QString> bindableName() { return QBindable<QString>(&name); } +}; + +#endif // ENEMY_H diff --git a/tests/auto/cmake/empty_qmldir/flee.qml b/tests/auto/cmake/empty_qmldir/flee.qml new file mode 100644 index 0000000000..3bc2b1ebbd --- /dev/null +++ b/tests/auto/cmake/empty_qmldir/flee.qml @@ -0,0 +1,7 @@ +import QtQml +import Third // Self import + +QtObject { + property int success: -11 + property Enemy other: Enemy { name: "evil" } +} diff --git a/tests/auto/cmake/empty_qmldir/main.cpp b/tests/auto/cmake/empty_qmldir/main.cpp new file mode 100644 index 0000000000..2f64641655 --- /dev/null +++ b/tests/auto/cmake/empty_qmldir/main.cpp @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtCore/qobject.h> +#include <QtQml/qqml.h> +#include <QtQml/qqmlengine.h> +#include <QtQml/qqmlcomponent.h> +#include <QtTest/qtest.h> + +class tst_empty_qmldir : public QObject +{ + Q_OBJECT +private slots: + void canSelfImport(); +}; + +void tst_empty_qmldir::canSelfImport() +{ + QQmlEngine engine; + QQmlComponent component(&engine, QUrl("qrc:/flee.qml")); + QScopedPointer<QObject> obj(component.create()); + QVERIFY2(!obj.isNull(), qPrintable(component.errorString())); + QCOMPARE(obj->property("success").toInt(), -11); + QObject *enemy = qvariant_cast<QObject *>(obj->property("other")); + QVERIFY(enemy != nullptr); + QCOMPARE(enemy->property("name").toString(), QStringLiteral("evil")); +} + +QTEST_MAIN(tst_empty_qmldir) + +#include "main.moc" |