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 <fabian.kosmale@qt.io>
(cherry picked from commit 1f9ab3767ed625a090af45f26dffd10dcd05eef5)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Thiago Macieira 2024-09-16 11:02:57 -07:00 committed by Qt Cherry-pick Bot
parent 228dfd5b31
commit cff3026140

View File

@ -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<QVariant>() && t != v.metaType()) {
if (isEnumType() && !t.metaObject() && v.metaType().id() == QMetaType::QString) {
if (isEnumType() && !t.metaObject() && v.metaType() == QMetaType::fromType<QString>()) {
// 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);