diff options
author | Ulf Hermann <[email protected]> | 2025-07-04 10:14:01 +0200 |
---|---|---|
committer | Ulf Hermann <[email protected]> | 2025-07-07 19:35:20 +0200 |
commit | 560bacbc9302b9622e8cabaf8dde2b3348480b3d (patch) | |
tree | 6373f111b67f7d199a374402647cb7cf04f8d999 | |
parent | 210f2c8a40460725b6d422b5d844576296ff3bf9 (diff) |
Flags declared with Q_FLAGS lack the methods for properly extracting the
typedef'd name. We need to manually register the typedef.
Amends commit 8bf5aae19b77b618f3f7a55a59e87c8a319475a8.
Pick-to: 6.10 6.9 6.8
Fixes: QTBUG-138174
Change-Id: I7c373f4d810a0c9a5590f39cc629015662a69ed4
Reviewed-by: Sami Shalayel <[email protected]>
-rw-r--r-- | src/qml/qml/qqml.h | 16 | ||||
-rw-r--r-- | src/qmltyperegistrar/qqmltyperegistrar.cpp | 4 | ||||
-rw-r--r-- | tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/auto/qml/qmlcppcodegen/data/enumproblems.h | 28 | ||||
-rw-r--r-- | tests/auto/qml/qmlcppcodegen/data/oldEnum.qml | 7 | ||||
-rw-r--r-- | tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp | 11 |
6 files changed, 65 insertions, 2 deletions
diff --git a/src/qml/qml/qqml.h b/src/qml/qml/qqml.h index d9b8ed66fe..250f8851b7 100644 --- a/src/qml/qml/qqml.h +++ b/src/qml/qml/qqml.h @@ -995,6 +995,22 @@ inline void qmlRegisterNamespaceAndRevisions(const QMetaObject *metaObject, classInfoMetaObject, nullptr); } +template<typename Enum> +void qmlRegisterEnum(const char *name) +{ + const QMetaType metaType = QMetaType::fromType<Enum>(); + + // Calling id() generally makes the metatype usable with fromName(). + metaType.id(); + + // If the enum was registered with the old Q_ENUMS or Q_FLAGS, + // we need to manually set up the typedef. + if constexpr (QtPrivate::IsQEnumHelper<Enum>::Value) + Q_UNUSED(name) + else + QMetaType::registerNormalizedTypedef(name, metaType); +} + int Q_QML_EXPORT qmlTypeId(const char *uri, int versionMajor, int versionMinor, const char *qmlName); QT_END_NAMESPACE diff --git a/src/qmltyperegistrar/qqmltyperegistrar.cpp b/src/qmltyperegistrar/qqmltyperegistrar.cpp index 635c7763fc..a9b80e0bd9 100644 --- a/src/qmltyperegistrar/qqmltyperegistrar.cpp +++ b/src/qmltyperegistrar/qqmltyperegistrar.cpp @@ -478,11 +478,11 @@ void QmlTypeRegistrar::write(QTextStream &output, QAnyStringView outFileName) co const auto enums = target.native.enums(); for (const auto &enumerator : enums) { output << uR"( - QMetaType::fromType<%1::%2>().id();)"_s.arg( + qmlRegisterEnum<%1::%2>("%1::%2");)"_s.arg( targetName, enumerator.name.toString()); if (!enumerator.alias.isEmpty()) { output << uR"( - QMetaType::fromType<%1::%2>().id();)"_s.arg( + qmlRegisterEnum<%1::%2>("%1::%2");)"_s.arg( targetName, enumerator.alias.toString()); } } diff --git a/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt b/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt index c3f24cd6a0..b83c7db544 100644 --- a/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt +++ b/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt @@ -250,6 +250,7 @@ set(qml_files objectInVar.qml objectLookupOnListElement.qml objectWithStringListMethod.qml + oldEnum.qml optionalComparison.qml outOfBounds.qml overriddenMember.qml diff --git a/tests/auto/qml/qmlcppcodegen/data/enumproblems.h b/tests/auto/qml/qmlcppcodegen/data/enumproblems.h index 9c98789e72..a224328b75 100644 --- a/tests/auto/qml/qmlcppcodegen/data/enumproblems.h +++ b/tests/auto/qml/qmlcppcodegen/data/enumproblems.h @@ -130,5 +130,33 @@ public: Q_ENUM(Bla) }; +class OldEnum : public QObject +{ + Q_OBJECT + Q_PROPERTY(Foos foos READ foos WRITE setFoos NOTIFY foosChanged) + + Q_ENUMS(Foo) + QML_ELEMENT +public: + enum Foo { None = 0, Bar = 13 }; + + Q_DECLARE_FLAGS(Foos, Foo) + Q_FLAGS(Foos) + + Foos foos() { return m_foos; } + void setFoos(Foos foos) + { + if (foos != m_foos) { + m_foos = foos; + emit foosChanged(); + } + } + +Q_SIGNALS: + void foosChanged(); + +private: + Foos m_foos = None; +}; #endif // ENUMPROBLEMS_H diff --git a/tests/auto/qml/qmlcppcodegen/data/oldEnum.qml b/tests/auto/qml/qmlcppcodegen/data/oldEnum.qml new file mode 100644 index 0000000000..39aa27cc21 --- /dev/null +++ b/tests/auto/qml/qmlcppcodegen/data/oldEnum.qml @@ -0,0 +1,7 @@ +pragma Strict +import TestTypes +import QtQml + +OldEnum { + foos: OldEnum.Bar +} diff --git a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp index f8b67ee7fb..b13c3f0755 100644 --- a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp +++ b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp @@ -212,6 +212,7 @@ private slots: void objectLookupOnListElement(); void objectToString(); void objectWithStringListMethod(); + void oldEnum(); void onAssignment(); void optionalComparison(); void outOfBoundsArray(); @@ -4305,6 +4306,16 @@ void tst_QmlCppCodegen::objectWithStringListMethod() QVERIFY(!o.isNull()); } +void tst_QmlCppCodegen::oldEnum() +{ + QQmlEngine engine; + QQmlComponent c(&engine, QUrl(u"qrc:/qt/qml/TestTypes/oldEnum.qml"_s)); + QVERIFY2(c.isReady(), qPrintable(c.errorString())); + QScopedPointer<QObject> o(c.create()); + QVERIFY(!o.isNull()); + QCOMPARE(o->property("foos").value<OldEnum::Foos>(), OldEnum::Bar); +} + void tst_QmlCppCodegen::onAssignment() { QQmlEngine engine; |