From 68923001279fd3da983dc59e57200e2080cadcb6 Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Tue, 29 Sep 2020 20:08:00 +0200 Subject: QVariant: Change metatype in convert() even on failure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The documentation of convert promised that “If the cast cannot be done, the variant is still changed to the requested type”. This was not the case so far, because we returned too early if canConvert returned false. This commit changes the behavior of the method to reflect its documentation. The documented behavior seems more useful than the alternative of not changing the metaType, at least for common use cases inside qtdeclarative. Change-Id: I09b5a5efb7344e76e93de278e35c7fb2b2f87dcd Reviewed-by: Thiago Macieira --- tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'tests/auto') diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index 0eefd95ac7..ea8f26ae20 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -99,6 +99,7 @@ private slots: void canConvert_data(); void canConvert(); + void convert(); void toSize_data(); void toSize(); @@ -555,6 +556,16 @@ void tst_QVariant::canConvert() QCOMPARE(val.canConvert(23876), false); } +void tst_QVariant::convert() +{ + // verify that after convert(), the variant's type has been changed + QVariant var = QVariant::fromValue(QString("A string")); + var.convert(QMetaType::fromType()); + QCOMPARE(var.metaType(), QMetaType::fromType()); + QCOMPARE(var.toInt(), 0); +} + + void tst_QVariant::toInt_data() { QTest::addColumn("value"); @@ -2343,8 +2354,8 @@ void tst_QVariant::qvariant_cast_QObject() QVERIFY(!data.canConvert(QMetaType::QObjectStar)); QVERIFY(!data.canConvert(::qMetaTypeId())); QVERIFY(!data.value()); - QVERIFY(!data.convert(QMetaType::QObjectStar)); QVERIFY(data.userType() != QMetaType::QObjectStar); + QVERIFY(!data.convert(QMetaType::QObjectStar)); } } -- cgit v1.2.3