QVariant: Fix isNull() == true after downcasting QObject*

[ChangeLog][QtCore][QVariant] Fixed a bug that caused isNull() to be
true after downcasting a QObject* payload using convert().

Fixes: QTBUG-73196
Change-Id: Ifda15952f873d7142c95609b69ac424bbf16b723
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@qt.io>
This commit is contained in:
Timo Lang 2019-03-20 09:50:51 +01:00
parent 6a210e0329
commit 758151d077
2 changed files with 10 additions and 0 deletions

View File

@ -1462,6 +1462,7 @@ static void customConstruct(QVariant::Private *d, const void *copy)
if (size <= sizeof(QVariant::Private::Data)
&& (type.flags() & (QMetaType::MovableType | QMetaType::IsEnumeration))) {
type.construct(&d->data.ptr, copy);
d->is_null = d->data.ptr == nullptr;
d->is_shared = false;
} else {
// Private::Data contains long long, and long double is the biggest standard type.
@ -1472,6 +1473,7 @@ static void customConstruct(QVariant::Private *d, const void *copy)
void *data = operator new(offset + size);
void *ptr = static_cast<char *>(data) + offset;
type.construct(ptr, copy);
d->is_null = ptr == nullptr;
d->is_shared = true;
d->data.shared = new (data) QVariant::PrivateShared(ptr);
}

View File

@ -2761,6 +2761,14 @@ void tst_QVariant::qvariant_cast_QObject_derived()
QCOMPARE(data.value<CustomQObjectDerived *>(), object);
QCOMPARE(data.value<CustomQObject *>(), object);
}
{
QObject *object = new CustomQObjectDerivedNoMetaType(this);
QVariant data = QVariant::fromValue(object);
QVERIFY(data.canConvert<CustomQObjectDerivedNoMetaType*>());
QVERIFY(data.convert(qMetaTypeId<CustomQObjectDerivedNoMetaType*>()));
QCOMPARE(data.value<CustomQObjectDerivedNoMetaType*>(), object);
QCOMPARE(data.isNull(), false);
}
}
struct QObjectWrapper