diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2022-08-25 15:59:39 -0300 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-09-01 19:51:39 +0000 |
commit | 2932130a8ee0157d3158a380924b7e9df8b52a2d (patch) | |
tree | 56cd7690c8cb505fbb1120e5e2afa6c7d5fa0736 | |
parent | Doc: Update QMetaType::metaObject() descriptions (diff) | |
download | qtbase-2932130a8ee0157d3158a380924b7e9df8b52a2d.tar.xz qtbase-2932130a8ee0157d3158a380924b7e9df8b52a2d.zip |
QVariant: fix conversions of string keys to Q_ENUM that are QFlags<>
Since Qt 6.0, QMetaType stores the name obtained from the C++ compiler,
which means we know a type like Qt::Alignment by its proper, full name
of QFlags<Qt::AlignmentFlag>. However, the meta object records only the
bare name of the enumeration, not the full flags.
Fixes: QTBUG-105932
Fixes: QTBUG-96185
Change-Id: Ic6547f8247454b47baa8fffd170eab977e306377
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit 98e21f0979143a1e278572390012f021dfed1b6a)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/corelib/kernel/qmetatype.cpp | 16 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp | 5 |
2 files changed, 16 insertions, 5 deletions
diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp index 51c234e90c..e35a8b3c31 100644 --- a/src/corelib/kernel/qmetatype.cpp +++ b/src/corelib/kernel/qmetatype.cpp @@ -1771,10 +1771,16 @@ static QMetaEnum metaEnumFromType(QMetaType t) { if (t.flags() & QMetaType::IsEnumeration) { if (const QMetaObject *metaObject = t.metaObject()) { - const QByteArray enumName = t.name(); - const char *lastColon = std::strrchr(enumName, ':'); - return metaObject->enumerator(metaObject->indexOfEnumerator( - lastColon ? lastColon + 1 : enumName.constData())); + QByteArrayView qflagsNamePrefix = "QFlags<"; + QByteArray enumName = t.name(); + if (enumName.endsWith('>') && enumName.startsWith(qflagsNamePrefix)) { + // extract the template argument + enumName.chop(1); + enumName = enumName.sliced(qflagsNamePrefix.size()); + } + if (qsizetype lastColon = enumName.lastIndexOf(':'); lastColon != -1) + enumName = enumName.sliced(lastColon + 1); + return metaObject->enumerator(metaObject->indexOfEnumerator(enumName)); } } return QMetaEnum(); @@ -2424,7 +2430,7 @@ bool QMetaType::canConvert(QMetaType fromType, QMetaType toType) return true; } const ConverterFunction * const f = - customTypesConversionRegistry()->function(qMakePair(fromTypeId, toTypeId)); + customTypesConversionRegistry()->function(std::make_pair(fromTypeId, toTypeId)); if (f) return true; diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index f20fd0cd87..e7f2c2bd9a 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -4715,6 +4715,11 @@ void tst_QVariant::metaEnums() METAENUMS_TEST(MetaEnumTest_Enum5_value); METAENUMS_TEST(MetaEnumTest_Enum6_value); METAENUMS_TEST(MetaEnumTest_Enum8_value); + +#undef METAENUMS_TEST + + testVariantMeta(Qt::RichText, &ok, "RichText"); + testVariantMeta(Qt::Alignment(Qt::AlignBottom), &ok, "AlignBottom"); } void tst_QVariant::nullConvert() |