summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/kernel/qproperty
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2021-04-29 14:08:50 +0200
committerLars Knoll <lars.knoll@qt.io>2021-06-01 16:29:15 +0200
commit6c1a9f2b4d4e0d2bcea0989c852ed1c653588154 (patch)
tree34365f5f390cb6d8fae16743adb23825caa68e2b /tests/auto/corelib/kernel/qproperty
parentde15836dbf5007092c19bc9ab15ca3d1a36901ad (diff)
Simplify storing of notification objects
QPropertyChangeHandler is a templated class and it's argument is a functor. That makes it inherently cumbersome to use the class in any context where the change handler needs to be stored. Introduce a QPropertyNotifier class that stores the functor in a std::function<void()>, and add a QProperty::addNotifier() method that can be used instead of onValueChanged(). Also make QPropertyNotifier default constructible. This significantly simplifies the code that needs to be written and makes it possible to store notifications as class members without major hassle. Fixes: QTBUG-92980 Change-Id: Id5b7baec093b9ac0467946cded943d92ad21030b Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto/corelib/kernel/qproperty')
-rw-r--r--tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp
index 4195459af5..d1dc0ceacf 100644
--- a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp
+++ b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp
@@ -108,6 +108,8 @@ private slots:
void groupedNotifications();
void groupedNotificationConsistency();
void uninstalledBindingDoesNotEvaluate();
+
+ void notify();
};
void tst_QProperty::functorBinding()
@@ -1774,6 +1776,36 @@ void tst_QProperty::uninstalledBindingDoesNotEvaluate()
QCOMPARE(bindingEvaluationCounter, 2);
}
+void tst_QProperty::notify()
+{
+ QProperty<int> testProperty(0);
+ QList<int> recordedValues;
+ int value = 0;
+ QPropertyNotifier notifier;
+
+ {
+ QPropertyNotifier handler = testProperty.addNotifier([&]() {
+ recordedValues << testProperty;
+ });
+ notifier = testProperty.addNotifier([&]() {
+ value = testProperty;
+ });
+
+ testProperty = 1;
+ testProperty = 2;
+ }
+ QCOMPARE(value, 2);
+ testProperty = 3;
+ QCOMPARE(value, 3);
+ notifier = {};
+ testProperty = 4;
+ QCOMPARE(value, 3);
+
+ QCOMPARE(recordedValues.count(), 2);
+ QCOMPARE(recordedValues.at(0), 1);
+ QCOMPARE(recordedValues.at(1), 2);
+}
+
QTEST_MAIN(tst_QProperty);
#undef QT_SOURCE_LOCATION_NAMESPACE