diff options
author | Andre Klitzing <aklitzing@gmail.com> | 2024-03-26 11:13:41 +0100 |
---|---|---|
committer | Andre Klitzing <aklitzing@gmail.com> | 2024-03-30 04:04:25 +0100 |
commit | 4be7779ded3377f9ef524567b255645b997639d2 (patch) | |
tree | cde986475d95ade2848840ed242307b9edf5cc50 | |
parent | 2d26d6127c75d4bce7c464e3d9f5b7caef5184e7 (diff) |
Fix wrong generation of Q_NAMESPACES
Generate Q_NAMESPACE if foreign type is a namespace.
Add compilation test for this only.
Fixes: QTBUG-123535
Pick-to: 6.5
Change-Id: Iacce6681a3fba468c1a9646630c871e2553d56bd
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
(cherry picked from commit f080d0309d4c1d0d8a96dabe832162c5f41b881f)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 4b1704b8e050755de7b86ab0f1b0dd262ca98982)
-rw-r--r-- | src/qmltyperegistrar/qmetatypesjsonprocessor.cpp | 15 | ||||
-rw-r--r-- | tests/auto/qml/qmltyperegistrar/CMakeLists.txt | 14 | ||||
-rw-r--r-- | tests/auto/qml/qmltyperegistrar/enum.cpp | 5 | ||||
-rw-r--r-- | tests/auto/qml/qmltyperegistrar/enum.h | 19 |
4 files changed, 50 insertions, 3 deletions
diff --git a/src/qmltyperegistrar/qmetatypesjsonprocessor.cpp b/src/qmltyperegistrar/qmetatypesjsonprocessor.cpp index 5bbd878779..df360bc44e 100644 --- a/src/qmltyperegistrar/qmetatypesjsonprocessor.cpp +++ b/src/qmltyperegistrar/qmetatypesjsonprocessor.cpp @@ -135,6 +135,7 @@ QString MetaTypesJsonProcessor::extractRegisteredTypes() const QString qmlAttached; bool isSingleton = false; bool isExplicitlyUncreatable = false; + bool isNamespace = obj[u"namespace"].toBool(); for (QJsonValue entry: classInfos) { const auto name = entry[u"name"].toString(); const auto value = entry[u"value"].toString(); @@ -159,8 +160,13 @@ QString MetaTypesJsonProcessor::extractRegisteredTypes() const if (qmlElement.isEmpty()) continue; // no relevant entries found const QString spaces = u" "_s; - registrationHelper += u"\nstruct "_s + foreignClassName + u"{\n Q_GADGET\n"_s; - registrationHelper += spaces + u"QML_FOREIGN(" + className + u")\n"_s; + if (isNamespace) { + registrationHelper += u"\nnamespace "_s + foreignClassName + u"{\n Q_NAMESPACE\n"_s; + registrationHelper += spaces + u"QML_FOREIGN_NAMESPACE(" + className + u")\n"_s; + } else { + registrationHelper += u"\nstruct "_s + foreignClassName + u"{\n Q_GADGET\n"_s; + registrationHelper += spaces + u"QML_FOREIGN(" + className + u")\n"_s; + } registrationHelper += spaces + qmlElement + u"\n"_s; if (isSingleton) registrationHelper += spaces + u"QML_SINGLETON\n"_s; @@ -172,7 +178,10 @@ QString MetaTypesJsonProcessor::extractRegisteredTypes() const } if (!qmlAttached.isEmpty()) registrationHelper += spaces + qmlAttached + u"\n"; - registrationHelper += u"};\n"; + registrationHelper += u"}"; + if (!isNamespace) + registrationHelper += u";"; + registrationHelper += u"\n"; } return registrationHelper; } diff --git a/tests/auto/qml/qmltyperegistrar/CMakeLists.txt b/tests/auto/qml/qmltyperegistrar/CMakeLists.txt index a0d26b733a..fadde11c95 100644 --- a/tests/auto/qml/qmltyperegistrar/CMakeLists.txt +++ b/tests/auto/qml/qmltyperegistrar/CMakeLists.txt @@ -93,3 +93,17 @@ qt_internal_add_resource(tst_qmltyperegistrar "resources" FILES duplicatedExports.json ) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) +qt_add_library(tst-qmltyperegistrar-enum-foreign STATIC enum.cpp) +qt_autogen_tools_initial_setup(tst-qmltyperegistrar-enum-foreign) +qt_enable_autogen_tool(tst-qmltyperegistrar-enum-foreign "moc" ON) +target_link_libraries(tst-qmltyperegistrar-enum-foreign PRIVATE Qt::QmlIntegration) + +qt_add_library(tst-qmltyperegistrar-enum STATIC) +qt_autogen_tools_initial_setup(tst-qmltyperegistrar-enum) +qt_enable_autogen_tool(tst-qmltyperegistrar-enum "moc" ON) +target_link_libraries(tst-qmltyperegistrar-enum PRIVATE Qt::Qml tst-qmltyperegistrar-enum-foreign) + +qt_add_qml_module(tst-qmltyperegistrar-enum URI TstEnum OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/TstEnum) +qt_generate_foreign_qml_types(tst-qmltyperegistrar-enum-foreign tst-qmltyperegistrar-enum) diff --git a/tests/auto/qml/qmltyperegistrar/enum.cpp b/tests/auto/qml/qmltyperegistrar/enum.cpp new file mode 100644 index 0000000000..34d2e00ffa --- /dev/null +++ b/tests/auto/qml/qmltyperegistrar/enum.cpp @@ -0,0 +1,5 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include "enum.h" +#include "moc_enum.cpp" diff --git a/tests/auto/qml/qmltyperegistrar/enum.h b/tests/auto/qml/qmltyperegistrar/enum.h new file mode 100644 index 0000000000..4a39c22545 --- /dev/null +++ b/tests/auto/qml/qmltyperegistrar/enum.h @@ -0,0 +1,19 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#ifndef ENUM_NS_HELLO_H +#define ENUM_NS_HELLO_H + +#include <QObject> +#include <QtQmlIntegration/qqmlintegration.h> + +namespace Hello { + Q_NAMESPACE + QML_NAMED_ELEMENT(World) + enum class World { + Europe = 2024, + }; + Q_ENUM_NS(World) +} + +#endif // ENUM_NS_HELLO_H |