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 --- src/corelib/kernel/qvariant.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/corelib/kernel') diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index f28cd5a7a0..31c2173072 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -2032,10 +2032,10 @@ bool QVariant::convert(QMetaType targetType) QVariant oldValue = *this; clear(); + create(targetType.id(), nullptr); if (!oldValue.canConvert(targetType)) return false; - create(targetType.id(), nullptr); // Fail if the value is not initialized or was forced null by a previous failed convert. if (oldValue.d.is_null && oldValue.d.typeId() != QMetaType::Nullptr) return false; -- cgit v1.2.3