aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2021-06-08 10:16:30 +0200
committerUlf Hermann <ulf.hermann@qt.io>2021-06-08 10:36:13 +0200
commitbdee2a862f9d9acc38b9a58b85a86fca0d2b38c2 (patch)
treefa86f96bb7e22b016d647fffe59ff9a715c1c483 /src
parent2bedff619c3775081bc5c8dfafa50fd7e11d8cb1 (diff)
qmltyperegistrar: Do not generate namespace metatypes for non-namespaces
If the target type of a QML_FOREIGN_NAMESPACE is not a namespace, we must not generate another metatype for it because otherwise we get two metatypes with the same name and ID. Change-Id: I0111e995a227c4a5d4c99e47662b7f86767e0da1 Reviewed-by: Andrei Golubev <andrei.golubev@qt.io> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/qmltyperegistrar/qmltyperegistrar.cpp49
1 files changed, 40 insertions, 9 deletions
diff --git a/src/qmltyperegistrar/qmltyperegistrar.cpp b/src/qmltyperegistrar/qmltyperegistrar.cpp
index 0091197384..914781e737 100644
--- a/src/qmltyperegistrar/qmltyperegistrar.cpp
+++ b/src/qmltyperegistrar/qmltyperegistrar.cpp
@@ -215,6 +215,7 @@ int main(int argc, char **argv)
auto moduleVersion = QTypeRevision::fromVersion(majorVersion.toInt(), minorVersion.toInt());
const QVector<QJsonObject> types = processor.types();
+ const QVector<QJsonObject> foreignTypes = processor.foreignTypes();
for (const QJsonObject &classDef : types) {
const QString className = classDef[QLatin1String("qualifiedClassName")].toString();
@@ -233,15 +234,45 @@ int main(int argc, char **argv)
// without including the C++ headers. That's the reason for the QMetaType(foo).id() calls.
if (classDef.value(QLatin1String("namespace")).toBool()) {
- fprintf(output, "\n {");
- fprintf(output, "\n static const auto metaType "
- "= QQmlPrivate::metaTypeForNamespace("
- "[](const QtPrivate::QMetaTypeInterface *) { "
- "return &%s::staticMetaObject; "
- "}, \"%s\");",
- qPrintable(targetName), qPrintable(targetName));
- fprintf(output, "\n QMetaType(&metaType).id();");
- fprintf(output, "\n }");
+ // We need to figure out if the _target_ is a namespace. If not, it already has a
+ // QMetaType and we don't need to generate one.
+
+ QString targetTypeName = targetName;
+ const auto targetIsNamespace = [&]() {
+ if (className == targetName)
+ return true;
+
+ const QJsonObject *target = QmlTypesClassDescription::findType(types, targetName);
+ if (!target)
+ target = QmlTypesClassDescription::findType(foreignTypes, targetName);
+
+ if (!target)
+ return false;
+
+ if (target->value(QStringLiteral("namespace")).toBool())
+ return true;
+
+ if (target->value(QStringLiteral("object")).toBool())
+ targetTypeName += QStringLiteral(" *");
+
+ return false;
+ };
+
+ if (targetIsNamespace()) {
+ fprintf(output, "\n {");
+ fprintf(output, "\n static const auto metaType "
+ "= QQmlPrivate::metaTypeForNamespace("
+ "[](const QtPrivate::QMetaTypeInterface *) { "
+ "return &%s::staticMetaObject; "
+ "}, \"%s\");",
+ qPrintable(targetName), qPrintable(targetTypeName));
+ fprintf(output, "\n QMetaType(&metaType).id();");
+ fprintf(output, "\n }");
+ } else {
+ fprintf(output, "\n QMetaType::fromType<%s>().id();",
+ qPrintable(targetTypeName));
+ }
+
if (seenQmlElement) {
fprintf(output, "\n qmlRegisterNamespaceAndRevisions(&%s::staticMetaObject, "
"\"%s\", %s, nullptr, &%s::staticMetaObject);",