summaryrefslogtreecommitdiffstats
path: root/src/corelib/doc/snippets/code
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2020-12-11 11:56:48 +0100
committerIvan Solovev <ivan.solovev@qt.io>2020-12-17 12:41:13 +0100
commitf0668433c4041a00399eeb29856273fce1389bf3 (patch)
tree9d45d68221c5b55a137887a166d7d3b00b5d8f84 /src/corelib/doc/snippets/code
parentf8de5e54022b8b7471131b7ad55c83b69b2684c0 (diff)
Bindable property with initialization
Implement Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS and Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS macros. They allow to directly initialize the property member. Task-number: QTBUG-85520 Change-Id: I76541d6785bbbf27976b9f0b865fb45be1e9beee 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.cpp39
1 files changed, 39 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 ed5df6eb86..4cb7c1c0ce 100644
--- a/src/corelib/doc/snippets/code/src_corelib_kernel_qproperty.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_kernel_qproperty.cpp
@@ -68,3 +68,42 @@ private:
Q_OBJECT_BINDABLE_PROPERTY(MyClass, int, xProp, &MyClass::xChanged)
};
//! [0]
+
+//! [1]
+class MyClass : public QObject
+{
+ Q_OBJECT
+ Q_PROPERTY(int x READ x WRITE setX NOTIFY xChanged BINDABLE bindableX)
+public:
+ int x() const { return xProp; }
+ void setX(int x) { xProp = x; }
+ QBindable<int> bindableX() { return QBindable<int>(&xProp); }
+
+signals:
+ void xChanged();
+
+private:
+ // Declare the instance of int bindable property data and
+ // initialize it with the value 5.
+ // This is similar to declaring
+ // int xProp = 5;
+ // without using the new QObjectBindableProperty class.
+ Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(MyClass, int, xProp, 5, &MyClass::xChanged)
+};
+//! [1]
+
+//! [2]
+class CustomType
+{
+public:
+ CustomType(int val, int otherVal) : value(val), anotherValue(otherVal) { }
+
+private:
+ int value = 0;
+ int anotherValue = 0;
+};
+
+// later when using CustomType as a property
+Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(MyClass, CustomType xProp, CustomType(5, 10),
+ &MyClass::xChanged)
+//! [2]