diff options
author | Fabian Kosmale <fabian.kosmale@qt.io> | 2021-03-07 20:26:56 +0100 |
---|---|---|
committer | Fabian Kosmale <fabian.kosmale@qt.io> | 2021-04-16 20:33:02 +0200 |
commit | e835bccb1e53c2eeb20b5f95a2c0ea4beb547b7c (patch) | |
tree | 2ddab4af3bab9cfbce9e00c387b3aaa96b98f59b /tests/auto | |
parent | a794c5e287381bd056008b20ae55f9b1e0acf138 (diff) |
QPropertyBinding: Add sticky mode
A sticky QPropertyBinding is a binding that does not get removed when a
write occurs. This is used in the QML engine to implement support for
the QQmlPropertyData::DontRemoveBinding flag.
Task-number: QTBUG-91689
Change-Id: Ib575b49abe634215318ccc7ba46212cc21eb4dad
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp index 0b05353bf0..c403276464 100644 --- a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp +++ b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp @@ -56,6 +56,7 @@ private slots: void avoidDependencyAllocationAfterFirstEval(); void boolProperty(); void takeBinding(); + void stickyBinding(); void replaceBinding(); void changeHandler(); void propertyChangeHandlerApi(); @@ -307,6 +308,30 @@ void tst_QProperty::takeBinding() QVERIFY(!existingBinding.isNull()); } +void tst_QProperty::stickyBinding() +{ + QProperty<int> prop; + QProperty<int> prop2 {2}; + prop.setBinding([&](){ return prop2.value(); }); + QCOMPARE(prop.value(), 2); + auto privBinding = QPropertyBindingPrivate::get(prop.binding()); + // If we make a binding sticky, + privBinding->setSticky(); + // then writing to the property does not remove it + prop = 1; + QVERIFY(prop.hasBinding()); + // but the value still changes. + QCOMPARE(prop.value(), 1); + // The binding continues to work normally. + prop2 = 3; + QCOMPARE(prop.value(), 3); + // If we remove the stickiness + privBinding->setSticky(false); + // the binding goes away on the next write + prop = 42; + QVERIFY(!prop.hasBinding()); +} + void tst_QProperty::replaceBinding() { QProperty<int> first(100); |