From 6a24ac7c4e0a7737b0cd0738c4e6fe8b9ac589ca Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Thu, 18 Jun 2020 16:55:48 +0200 Subject: QNotifiedProperty: pass old value to callback if requested Check at compile time whether the static callback takes an argument (which has to be of the same time as the type of the property). If so, retrieve the old value and pass it to the callback. Change-Id: Ib1c4c9e05b826b6be492b03f66fa72ad015963ee Reviewed-by: Ulf Hermann --- .../corelib/kernel/qproperty/tst_qproperty.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'tests') diff --git a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp index e606d121ec..7200bf176e 100644 --- a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp +++ b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp @@ -72,6 +72,7 @@ private slots: void multipleObservers(); void propertyAlias(); void notifiedProperty(); + void notifiedPropertyWithOldValueCallback(); }; void tst_QProperty::functorBinding() @@ -779,6 +780,15 @@ struct ClassWithNotifiedProperty QNotifiedProperty property; }; +struct ClassWithNotifiedProperty2 +{ + QVector recordedValues; + + void callback(int oldValue) { recordedValues << oldValue; } + + QNotifiedProperty property; +}; + void tst_QProperty::notifiedProperty() { ClassWithNotifiedProperty instance; @@ -854,6 +864,18 @@ void tst_QProperty::notifiedProperty() QCOMPARE(subscribedCount, 10); } +void tst_QProperty::notifiedPropertyWithOldValueCallback() +{ + ClassWithNotifiedProperty2 instance; + instance.property.setValue(&instance, 1); + instance.property.setBinding(&instance, [](){return 2;}); + instance.property.setBinding(&instance, [](){return 3;}); + instance.property.setValue(&instance, 4); + QVector expected {0, 1, 2, 3}; + QCOMPARE(instance.recordedValues, expected); + QCOMPARE(instance.property.value(), 4); +} + QTEST_MAIN(tst_QProperty); #include "tst_qproperty.moc" -- cgit v1.2.3