summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2018-08-03 17:55:11 -0700
committerThiago Macieira <thiago.macieira@intel.com>2018-08-21 04:59:13 +0000
commite0c798742adbc984e71c0f65cc432101da5b9c52 (patch)
tree9d7ec9d2fc58d0080cd9d33c79b09f8d2b321edc
parentb5221dff520efa3bb923c4faeeeb5712f7724a87 (diff)
QObject: do allow setProperty() to change the type of the property
[ChangeLog][QtCore][QObject] Fixed a bug in setProperty() that caused a property change not to take effect if the old value compared equal using QVariant's equality operator, but the values were not strictly equal. Task-number: QTBUG-69744 Change-Id: I00e04a465fcf4fc1a462fffd1547885861a07a64 Reviewed-by: Simon Hausmann <simon.hausmann@qt.io> Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com> (cherry picked from commit 64a560d977a0a511ef541d6116d82e7b5c911a92)
-rw-r--r--src/corelib/kernel/qobject.cpp3
-rw-r--r--tests/auto/corelib/kernel/qobject/tst_qobject.cpp14
2 files changed, 15 insertions, 2 deletions
diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp
index 57711389c4..adc85f9a3e 100644
--- a/src/corelib/kernel/qobject.cpp
+++ b/src/corelib/kernel/qobject.cpp
@@ -3894,7 +3894,8 @@ bool QObject::setProperty(const char *name, const QVariant &value)
d->extraData->propertyNames.append(name);
d->extraData->propertyValues.append(value);
} else {
- if (value == d->extraData->propertyValues.at(idx))
+ if (value.userType() == d->extraData->propertyValues.at(idx).userType()
+ && value == d->extraData->propertyValues.at(idx))
return false;
d->extraData->propertyValues[idx] = value;
}
diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp
index f1e58921c0..5d8e921fa0 100644
--- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp
+++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp
@@ -2975,6 +2975,7 @@ void tst_QObject::dynamicProperties()
QVERIFY(obj.dynamicPropertyNames().isEmpty());
+ // set a non-dynamic property
QVERIFY(obj.setProperty("number", 42));
QVERIFY(obj.changedDynamicProperties.isEmpty());
QCOMPARE(obj.property("number").toInt(), 42);
@@ -2982,19 +2983,30 @@ void tst_QObject::dynamicProperties()
QVERIFY(!obj.setProperty("number", "invalid string"));
QVERIFY(obj.changedDynamicProperties.isEmpty());
+ // set a dynamic property
QVERIFY(!obj.setProperty("myuserproperty", "Hello"));
QCOMPARE(obj.changedDynamicProperties.count(), 1);
QCOMPARE(obj.changedDynamicProperties.first(), QByteArray("myuserproperty"));
//check if there is no redundant DynamicPropertyChange events
QVERIFY(!obj.setProperty("myuserproperty", "Hello"));
QCOMPARE(obj.changedDynamicProperties.count(), 1);
- obj.changedDynamicProperties.clear();
+ QCOMPARE(obj.property("myuserproperty").type(), QVariant::String);
QCOMPARE(obj.property("myuserproperty").toString(), QString("Hello"));
QCOMPARE(obj.dynamicPropertyNames().count(), 1);
QCOMPARE(obj.dynamicPropertyNames().first(), QByteArray("myuserproperty"));
+ // change type of the dynamic property
+ obj.changedDynamicProperties.clear();
+ QVERIFY(!obj.setProperty("myuserproperty", QByteArray("Hello")));
+ QCOMPARE(obj.changedDynamicProperties.count(), 1);
+ QCOMPARE(obj.changedDynamicProperties.first(), QByteArray("myuserproperty"));
+ QCOMPARE(obj.property("myuserproperty").type(), QVariant::ByteArray);
+ QCOMPARE(obj.property("myuserproperty").toByteArray(), QByteArray("Hello"));
+
+ // unset the property
+ obj.changedDynamicProperties.clear();
QVERIFY(!obj.setProperty("myuserproperty", QVariant()));
QCOMPARE(obj.changedDynamicProperties.count(), 1);