From cff302614051dd5a668f0b166585898e6e64f07c Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 16 Sep 2024 11:02:57 -0700 Subject: [PATCH] QMetaProperty: fix type-punning of QFlags in write() from QString Amends commit 46f407126ef3e94d59254012cdc34d6a4ad2faf2 ("MetaObject: store the QMetaType of the properties"), from 6.0 cycle, which made QMetaProperty::{read,write}() use the actual enum type in the QVariant. But it missed the case where the input QVariant contained a string with the enum's values. Note this is *not* a type-punning in all cases: if the type of the property was marked as a Q_FLAG, moc generates code to access it as int. That is fixed in the next commit. Drive-by compare QMetaTypes instead of their IDs, which avoids generating a call to register them. Pick-to: 6.5 See-also: https://lists.qt-project.org/pipermail/development/2024-September/045636.html Change-Id: I1cfce869090c96bb41b6fffdc20855cfa7cb2a18 Reviewed-by: Fabian Kosmale (cherry picked from commit 1f9ab3767ed625a090af45f26dffd10dcd05eef5) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/kernel/qmetaobject.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp index 05662b385a5..f9df95ebce3 100644 --- a/src/corelib/kernel/qmetaobject.cpp +++ b/src/corelib/kernel/qmetaobject.cpp @@ -3775,8 +3775,10 @@ bool QMetaProperty::write(QObject *object, QVariant &&v) const return false; QMetaType t(mobj->d.metaTypes[data.index(mobj)]); if (t != QMetaType::fromType() && t != v.metaType()) { - if (isEnumType() && !t.metaObject() && v.metaType().id() == QMetaType::QString) { + if (isEnumType() && !t.metaObject() && v.metaType() == QMetaType::fromType()) { // Assigning a string to a property of type Q_ENUMS (instead of Q_ENUM) + // means the QMetaType has no associated QMetaObject, so it can't + // do the conversion (see qmetatype.cpp:convertToEnum()). bool ok; if (isFlagType()) v = QVariant(menum.keysToValue(v.toByteArray(), &ok)); @@ -3784,7 +3786,8 @@ bool QMetaProperty::write(QObject *object, QVariant &&v) const v = QVariant(menum.keyToValue(v.toByteArray(), &ok)); if (!ok) return false; - } else if (!v.isValid()) { + } + if (!v.isValid()) { if (isResettable()) return reset(object); v = QVariant(t, nullptr);