Fix QVariant conversion to an enum type.
QVariant::canConvert<Enum> was returning true for everything can can be converted to integer, but not for integer itself. That's because in QVariant::canConvert we set the targetType to Int of it's an enum, but the Int->Int case was not on the conversion matrix. So this commits adds it to the conversion matrix and now QVariant::canConvert<Enum> returns consistently true for int itself. But even tough canConvert returned true, it did not actualy do any conversion to the enum type itself. Fix that by handling the case properlt in 'convert' [ChangeLog][QtCore][QVariant] Fixed QVariant::canConvert and conversion from integer types to enumeration types. Task-number: QTBUG-53384 Change-Id: I6ac066f3900e31bfcea7af77836ddfc7730bd60b Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
474af0a61d
commit
45eba73492
@ -945,6 +945,26 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if (QMetaType::typeFlags(t) & QMetaType::IsEnumeration) {
|
||||
qlonglong value = qConvertToNumber(d, ok);
|
||||
if (*ok) {
|
||||
switch (QMetaType::sizeOf(t)) {
|
||||
case 1:
|
||||
*static_cast<signed char *>(result) = value;
|
||||
return true;
|
||||
case 2:
|
||||
*static_cast<qint16 *>(result) = value;
|
||||
return true;
|
||||
case 4:
|
||||
*static_cast<qint32 *>(result) = value;
|
||||
return true;
|
||||
case 8:
|
||||
*static_cast<qint64 *>(result) = value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return *ok;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@ -2819,7 +2839,7 @@ static const quint32 qCanConvertMatrix[QVariant::LastCoreType + 1] =
|
||||
|
||||
/*Int*/ 1 << QVariant::UInt | 1 << QVariant::String | 1 << QVariant::Double
|
||||
| 1 << QVariant::Bool | 1 << QVariant::LongLong | 1 << QVariant::ULongLong
|
||||
| 1 << QVariant::Char | 1 << QVariant::ByteArray,
|
||||
| 1 << QVariant::Char | 1 << QVariant::ByteArray | 1 << QVariant::Int,
|
||||
|
||||
/*UInt*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
|
||||
| 1 << QVariant::Bool | 1 << QVariant::LongLong | 1 << QVariant::ULongLong
|
||||
|
@ -4629,6 +4629,16 @@ template<typename Enum> void testVariant(Enum value, bool *ok)
|
||||
QVERIFY(var2.convert(QMetaType::Int));
|
||||
QCOMPARE(var2.value<int>(), static_cast<int>(value));
|
||||
|
||||
if (static_cast<qint64>(value) <= INT_MAX) {
|
||||
int intValue = static_cast<int>(value);
|
||||
QVariant intVar = intValue;
|
||||
QVERIFY(intVar.canConvert<Enum>());
|
||||
QCOMPARE(intVar.value<Enum>(), value);
|
||||
}
|
||||
qint64 longValue = static_cast<qint64>(value);
|
||||
QVERIFY(QVariant(longValue).canConvert<Enum>());
|
||||
QCOMPARE(QVariant(longValue).value<Enum>(), value);
|
||||
|
||||
*ok = true;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user