diff options
author | Andreas Buhr <andreas.buhr@qt.io> | 2021-01-26 13:21:39 +0100 |
---|---|---|
committer | Andreas Buhr <andreas.buhr@qt.io> | 2021-03-25 14:52:37 +0100 |
commit | 96796f0619baf44e0b2a832c92893e6f823f1d1d (patch) | |
tree | 333997791edca96c7c4474414c6faf7e8c20c83c /src/corelib/doc/snippets/code | |
parent | 14eeb520858b624a2c9e09b5bd95ea8fb90a1f5a (diff) |
Add example to QObjectBindableProperty change handler docs
QObjectBindableProperty has a callback, which is called whenever
the value changes. This patch adds an example showing this.
Task-number: QTBUG-90511
Change-Id: I56c0bce15af8121159630b5c0922c287c15b7618
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/corelib/doc/snippets/code')
-rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_kernel_qproperty.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_kernel_qproperty.cpp b/src/corelib/doc/snippets/code/src_corelib_kernel_qproperty.cpp index a402a70599..3d63abe590 100644 --- a/src/corelib/doc/snippets/code/src_corelib_kernel_qproperty.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_kernel_qproperty.cpp @@ -119,3 +119,44 @@ void usage_QBindable() { qDebug() << bindableX.hasBinding() << myObject->x(); // prints true 84 //! [3] } + +//! [4] +#include <QObject> +#include <QProperty> +#include <QDebug> + +class Foo : public QObject +{ + Q_OBJECT + Q_PROPERTY(int myVal READ myVal WRITE setMyVal BINDABLE bindableMyVal) +public: + int myVal() { return myValMember.value(); } + void setMyVal(int newvalue) { myValMember = newvalue; } + QBindable<int> bindableMyVal() { return &myValMember; } +signals: + void myValChanged(); + +private: + Q_OBJECT_BINDABLE_PROPERTY(Foo, int, myValMember, &Foo::myValChanged); +}; + +int main() +{ + bool debugout(true); // enable debug log + Foo myfoo; + QProperty<int> prop(42); + QObject::connect(&myfoo, &Foo::myValChanged, [&]() { + if (debugout) + qDebug() << myfoo.myVal(); + }); + myfoo.bindableMyVal().setBinding([&]() { return prop.value(); }); // prints "42" + + prop = 5; // prints "5" + debugout = false; + prop = 6; // prints nothing + debugout = true; + prop = 7; // prints "7" +} + +#include "main.moc" +//! [4] |