diff options
-rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_kernel_qproperty.cpp | 41 | ||||
-rw-r--r-- | src/corelib/kernel/qproperty.cpp | 4 |
2 files changed, 45 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] diff --git a/src/corelib/kernel/qproperty.cpp b/src/corelib/kernel/qproperty.cpp index 30ad3392c0..8cefccdd63 100644 --- a/src/corelib/kernel/qproperty.cpp +++ b/src/corelib/kernel/qproperty.cpp @@ -1130,6 +1130,10 @@ QString QPropertyBindingError::description() const In order to invoke the change signal on property changes, use QObjectBindableProperty and pass the change signal as a callback. + A simple example is given in the following. + + \snippet code/src_corelib_kernel_qproperty.cpp 4 + QObjectBindableProperty is usually not used directly, instead an instance of it is created by using the Q_OBJECT_BINDABLE_PROPERTY macro. |