From 96796f0619baf44e0b2a832c92893e6f823f1d1d Mon Sep 17 00:00:00 2001 From: Andreas Buhr Date: Tue, 26 Jan 2021 13:21:39 +0100 Subject: 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 --- .../snippets/code/src_corelib_kernel_qproperty.cpp | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src/corelib/doc/snippets') 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 +#include +#include + +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 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 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] -- cgit v1.2.3