QVariant: implement QByteArray ↔ QUuid conversion

Seems like an obvious omission.

[ChangeLog][QtCore][QVariant] Can now convert QUuid to and from
QByteArray, not just QString.

Change-Id: Ib56ae86ca0c27adaf1e095b6b85e64fe64ea8d18
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2017-04-09 08:41:51 +02:00
parent 097073fa67
commit e1c8451ffe
3 changed files with 29 additions and 6 deletions

View File

@ -649,6 +649,9 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
case QVariant::Bool:
*ba = QByteArray(d->data.b ? "true" : "false");
break;
case QVariant::Uuid:
*ba = v_cast<QUuid>(d)->toByteArray();
break;
default:
#ifndef QT_NO_QOBJECT
{
@ -916,6 +919,9 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
case QVariant::String:
*static_cast<QUuid *>(result) = QUuid(*v_cast<QString>(d));
break;
case QVariant::ByteArray:
*static_cast<QUuid *>(result) = QUuid(*v_cast<QByteArray>(d));
break;
default:
return false;
}
@ -2527,7 +2533,7 @@ QRegularExpression QVariant::toRegularExpression() const
\since 5.0
Returns the variant as a QUuid if the variant has type()
\l QMetaType::QUuid or \l QMetaType::QString;
\l QMetaType::QUuid, \l QMetaType::QByteArray or \l QMetaType::QString;
otherwise returns a default-constructed QUuid.
\sa canConvert(), convert()
@ -2875,7 +2881,8 @@ static const quint32 qCanConvertMatrix[QVariant::LastCoreType + 1] =
/*QStringList*/ 1 << QVariant::List | 1 << QVariant::String,
/*QByteArray*/ 1 << QVariant::String | 1 << QVariant::Int | 1 << QVariant::UInt | 1 << QVariant::Bool
| 1 << QVariant::Double | 1 << QVariant::LongLong | 1 << QVariant::ULongLong,
| 1 << QVariant::Double | 1 << QVariant::LongLong | 1 << QVariant::ULongLong
| 1 << QVariant::Uuid,
/*QBitArray*/ 0,
@ -2911,7 +2918,7 @@ static const quint32 qCanConvertMatrix[QVariant::LastCoreType + 1] =
/*QEasingCurve*/ 0,
/*QUuid*/ 1 << QVariant::String
/*QUuid*/ 1 << QVariant::String | 1 << QVariant::ByteArray,
};
static const size_t qCanConvertMatrixMaximumTargetType = 8 * sizeof(*qCanConvertMatrix);
@ -2966,7 +2973,7 @@ static bool canConvertMetaObject(int fromId, int toId, QObject *fromObject)
\l QMetaType::UInt, \l QMetaType::ULongLong
\row \li \l QMetaType::QByteArray \li \l QMetaType::Double,
\l QMetaType::Int, \l QMetaType::LongLong, \l QMetaType::QString,
\l QMetaType::UInt, \l QMetaType::ULongLong
\l QMetaType::UInt, \l QMetaType::ULongLong, \l QMetaType::QUuid
\row \li \l QMetaType::QChar \li \l QMetaType::Bool, \l QMetaType::Int,
\l QMetaType::UInt, \l QMetaType::LongLong, \l QMetaType::ULongLong
\row \li \l QMetaType::QColor \li \l QMetaType::QString
@ -3006,7 +3013,7 @@ static bool canConvertMetaObject(int fromId, int toId, QObject *fromObject)
\row \li \l QMetaType::ULongLong \li \l QMetaType::Bool,
\l QMetaType::QChar, \l QMetaType::Double, \l QMetaType::Int,
\l QMetaType::LongLong, \l QMetaType::QString, \l QMetaType::UInt
\row \li \l QMetaType::QUuid \li \l QMetaType::QString
\row \li \l QMetaType::QUuid \li \l QMetaType::QByteArray, \l QMetaType::QString
\endtable
A QVariant containing a pointer to a type derived from QObject will also return true for this

View File

@ -1512,10 +1512,11 @@ void tst_QVariant::operator_eq_eq_data()
// ### many other combinations missing
{
// QUuid can convert to QString, but not the opposite
QUuid uuid(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
QTest::newRow("uuidstring") << QVariant(uuid) << QVariant(uuid.toString()) << true;
QTest::newRow("stringuuid") << QVariant(uuid.toString()) << QVariant(uuid) << true;
QTest::newRow("uuidbytearray") << QVariant(uuid) << QVariant(uuid.toByteArray()) << true;
QTest::newRow("bytearrayuuid") << QVariant(uuid.toByteArray()) << QVariant(uuid) << true;
}
{

View File

@ -392,9 +392,16 @@ void tst_QUuid::qvariant_conversion()
QUuid uuid = QUuid::createUuid();
QVariant v = QVariant::fromValue(uuid);
// QUuid -> QString
QVERIFY(v.canConvert<QString>());
QCOMPARE(v.toString(), uuid.toString());
QCOMPARE(v.value<QString>(), uuid.toString());
// QUuid -> QByteArray
QVERIFY(v.canConvert<QByteArray>());
QCOMPARE(v.toByteArray(), uuid.toByteArray());
QCOMPARE(v.value<QByteArray>(), uuid.toByteArray());
QVERIFY(!v.canConvert<int>());
QVERIFY(!v.canConvert<QStringList>());
@ -403,6 +410,14 @@ void tst_QUuid::qvariant_conversion()
QCOMPARE(sv.type(), QVariant::String);
QVERIFY(sv.canConvert<QUuid>());
QCOMPARE(sv.value<QUuid>(), uuid);
// QString -> QUuid
{
QVariant sv = QVariant::fromValue(uuid.toByteArray());
QCOMPARE(sv.type(), QVariant::ByteArray);
QVERIFY(sv.canConvert<QUuid>());
QCOMPARE(sv.value<QUuid>(), uuid);
}
}
void tst_QUuid::darwinTypes()