QVariant: fix conversions of Q_ENUM that are QFlags<> to string

The doc of QMetaEnum::valueToKey() says to use ::valueToKeys() instead
for flag types.

Pick-to: 6.4
Change-Id: I48e5ba47324137f2ce2710f1d876e93e7c562e9f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Eirik Aavitsland 2022-08-29 17:48:27 +02:00
parent ac22743f21
commit 54aa7e75b8
2 changed files with 15 additions and 5 deletions
src/corelib/kernel
tests/auto/corelib/kernel/qvariant

View File

@ -1945,11 +1945,19 @@ static bool convertFromEnum(QMetaType fromType, const void *from, QMetaType toTy
#ifndef QT_NO_QOBJECT
QMetaEnum en = metaEnumFromType(fromType);
if (en.isValid()) {
const char *key = en.valueToKey(ll);
if (toType.id() == QMetaType::QString)
*static_cast<QString *>(to) = QString::fromUtf8(key);
else
*static_cast<QByteArray *>(to) = key;
if (en.isFlag()) {
const QByteArray keys = en.valueToKeys(ll);
if (toType.id() == QMetaType::QString)
*static_cast<QString *>(to) = QString::fromUtf8(keys);
else
*static_cast<QByteArray *>(to) = keys;
} else {
const char *key = en.valueToKey(ll);
if (toType.id() == QMetaType::QString)
*static_cast<QString *>(to) = QString::fromUtf8(key);
else
*static_cast<QByteArray *>(to) = key;
}
return true;
}
#endif

View File

@ -4753,6 +4753,8 @@ void tst_QVariant::metaEnums()
testVariantMeta(Qt::RichText, &ok, "RichText");
testVariantMeta(Qt::Alignment(Qt::AlignBottom), &ok, "AlignBottom");
testVariantMeta(Qt::Alignment(Qt::AlignHCenter | Qt::AlignBottom), &ok,
"AlignHCenter|AlignBottom");
}
void tst_QVariant::nullConvert()