diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2020-05-25 12:51:39 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2020-05-27 11:54:30 +0200 |
commit | 524d78160726b25ed424a2c7a6d5e423b7ea4b93 (patch) | |
tree | c5ca373ef8231454dab4a73ffe2d479a73f103aa /tests/auto/corelib/kernel | |
parent | 36bd34dbdcaa0fcfd47d1edb3494a4babb709ca3 (diff) |
QProperty: Support multiple observers
Previously, only the first observer would get notified. Also, make sure
that the notifiers are always retained when switching between bindings
and values.
Change-Id: I9c25c0f2e288dac3a335b68e618f7ddeb44be25a
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto/corelib/kernel')
-rw-r--r-- | tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp index 2cfcc8e22f..a8846e0987 100644 --- a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp +++ b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp @@ -69,6 +69,7 @@ private slots: void genericPropertyBindingBool(); void staticChangeHandler(); void setBindingFunctor(); + void multipleObservers(); }; void tst_QProperty::functorBinding() @@ -686,6 +687,41 @@ void tst_QProperty::setBindingFunctor() QCOMPARE(property.value(), 200); } +void tst_QProperty::multipleObservers() +{ + QProperty<int> property; + property.setValue(5); + QCOMPARE(property.value(), 5); + + int value1 = 1; + auto changeHandler = property.onValueChanged([&]() { value1 = property.value(); }); + QCOMPARE(value1, 1); + + int value2 = 2; + auto subscribeHandler = property.subscribe([&]() { value2 = property.value(); }); + QCOMPARE(value2, 5); + + property.setValue(6); + QCOMPARE(property.value(), 6); + QCOMPARE(value1, 6); + QCOMPARE(value2, 6); + + property.setBinding([]() { return 12; }); + QCOMPARE(value1, 12); + QCOMPARE(value2, 12); + QCOMPARE(property.value(), 12); + + property.setBinding(QPropertyBinding<int>()); + QCOMPARE(value1, 12); + QCOMPARE(value2, 12); + QCOMPARE(property.value(), 12); + + property.setValue(22); + QCOMPARE(value1, 22); + QCOMPARE(value2, 22); + QCOMPARE(property.value(), 22); +} + QTEST_MAIN(tst_QProperty); #include "tst_qproperty.moc" |