Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
src_corelib_kernel_qproperty.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QObject>
5#include <QDebug>
6#include <QObjectBindableProperty>
7
9{
10 //! [0]
11 class MyClass : public QObject
12 {
13 Q_OBJECT
14 Q_PROPERTY(int x READ x WRITE setX NOTIFY xChanged BINDABLE bindableX)
15 public:
16 int x() const { return xProp; }
17 void setX(int x) { xProp = x; }
18 QBindable<int> bindableX() { return QBindable<int>(&xProp); }
19
20 signals:
21 void xChanged();
22
23 private:
24 // Declare the instance of the bindable property data.
25 Q_OBJECT_BINDABLE_PROPERTY(MyClass, int, xProp, &MyClass::xChanged)
26 };
27 //! [0]
28}
29
30//! [1]
31class MyClass : public QObject
32{
33 Q_OBJECT
34 Q_PROPERTY(int x READ x WRITE setX NOTIFY xChanged BINDABLE bindableX)
35public:
36 int x() const { return xProp; }
37 void setX(int x) { xProp = x; }
38 QBindable<int> bindableX() { return QBindable<int>(&xProp); }
39
41 void xChanged();
42
43private:
44 // Declare the instance of int bindable property data and
45 // initialize it with the value 5.
46 // This is similar to declaring
47 // int xProp = 5;
48 // without using the new QObjectBindableProperty class.
49 Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(MyClass, int, xProp, 5, &MyClass::xChanged)
50};
51//! [1]
52
53//! [2]
55{
56public:
57 CustomType(int val, int otherVal) : value(val), anotherValue(otherVal) { }
58
59private:
60 int value = 0;
61 int anotherValue = 0;
62};
63
64//! [2]
65
66class SomeClass : public QObject
67{
68 Q_OBJECT
69 Q_PROPERTY(int x READ x WRITE setX NOTIFY xChanged BINDABLE bindableX)
70
71private:
72 //! [2_property_use]
73 // later when using CustomType as a property
76 //! [2_property_use]
77};
78
80 //! [3]
81 MyClass *myObject;
82 QBindable<int> bindableX = myObject->bindableX();
83 qDebug() << bindableX.hasBinding(); // prints false
84 QProperty<int> y {42};
85 bindableX.setBinding([&](){ return 2*y.value(); });
86 qDebug() << bindableX.hasBinding() << myObject->x(); // prints true 84
87 //! [3]
88}
89
90//! [4]
91#include <QObject>
92#include <QProperty>
93#include <QDebug>
94
95class Foo : public QObject
96{
97 Q_OBJECT
98 Q_PROPERTY(int myVal READ myVal WRITE setMyVal BINDABLE bindableMyVal)
99public:
100 int myVal() { return myValMember.value(); }
103signals:
105
106private:
107 Q_OBJECT_BINDABLE_PROPERTY(Foo, int, myValMember, &Foo::myValChanged);
108};
109
110int main()
111{
112 bool debugout(true); // enable debug log
113 Foo myfoo;
114 QProperty<int> prop(42);
115 QObject::connect(&myfoo, &Foo::myValChanged, [&]() {
116 if (debugout)
117 qDebug() << myfoo.myVal();
118 });
119 myfoo.bindableMyVal().setBinding([&]() { return prop.value(); }); // prints "42"
120
121 prop = 5; // prints "5"
122 debugout = false;
123 prop = 6; // prints nothing
124 debugout = true;
125 prop = 7; // prints "7"
126}
127
128//! [4]
129
130#if 0
131//! [4_include_moc]
132#include "main.moc"
133//! [4_include_moc]
134#endif
135
137{
138 //! [5]
139 class Client{};
140
141 class MyClassPrivate : public QObject
142 {
143 public:
145 bool hasClientsActualCalculation() const { return clients.size() > 0; }
146 Q_OBJECT_COMPUTED_PROPERTY(MyClassPrivate, bool, hasClientsData,
147 &MyClassPrivate::hasClientsActualCalculation)
148 };
149
150 class MyClass : public QObject
151 {
152 Q_OBJECT
153 Q_PROPERTY(bool hasClients READ hasClients STORED false BINDABLE bindableHasClients)
154 public:
156 {
157 return QBindable<bool>(&d_func()->hasClientsData);
158 }
159 bool hasClients() const
160 {
161 return d_func()->hasClientsData.value();
162 }
163 void addClient(const Client &c)
164 {
165 Q_D(MyClass);
167 // notify that the value could have changed
169 }
170 private:
172 };
173 //! [5]
174}
CustomType(int val, int otherVal)
int main()
[open]