Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
8
namespace
MyClassExample
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]
31
class
MyClass
:
public
QObject
32
{
33
Q_OBJECT
34
Q_PROPERTY(
int
x READ x WRITE setX NOTIFY xChanged BINDABLE bindableX)
35
public
:
36
int
x
()
const
{
return
xProp
; }
37
void
setX
(
int
x
) {
xProp
=
x
; }
38
QBindable
<
int
>
bindableX
() {
return
QBindable
<
int
>(&
xProp
); }
39
40
signals
:
41
void
xChanged
();
42
43
private
:
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]
54
class
CustomType
55
{
56
public
:
57
CustomType
(
int
val,
int
otherVal) : value(val), anotherValue(otherVal) { }
58
59
private
:
60
int
value = 0;
61
int
anotherValue = 0;
62
};
63
64
//! [2]
65
66
class
SomeClass
:
public
QObject
67
{
68
Q_OBJECT
69
Q_PROPERTY(
int
x READ x WRITE setX NOTIFY xChanged BINDABLE bindableX)
70
71
private
:
72
//! [2_property_use]
73
// later when using CustomType as a property
74
Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS
(
SomeClass
,
CustomType
,
xProp
,
CustomType
(5, 10),
75
&
MyClass
::
xChanged
)
76
//! [2_property_use]
77
};
78
79
void
usage_QBindable
() {
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
95
class
Foo
:
public
QObject
96
{
97
Q_OBJECT
98
Q_PROPERTY(
int
myVal READ myVal WRITE setMyVal BINDABLE bindableMyVal)
99
public
:
100
int
myVal
() {
return
myValMember
.
value
(); }
101
void
setMyVal
(
int
newvalue
) {
myValMember
=
newvalue
; }
102
QBindable
<
int
>
bindableMyVal
() {
return
&
myValMember
; }
103
signals
:
104
void
myValChanged
();
105
106
private
:
107
Q_OBJECT_BINDABLE_PROPERTY(
Foo
,
int
, myValMember, &Foo::myValChanged);
108
};
109
110
int
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
136
namespace
MyClassClientExample
137
{
138
//! [5]
139
class
Client
{};
140
141
class
MyClassPrivate
:
public
QObject
142
{
143
public
:
144
QList
<
Client
>
clients
;
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
:
155
QBindable
<
bool
>
bindableHasClients
()
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
);
166
d
->
clients
.
push_back
(
c
);
167
// notify that the value could have changed
168
d
->
hasClientsData
.
notify
();
169
}
170
private
:
171
Q_DECLARE_PRIVATE
(
MyClass
)
172
};
173
//! [5]
174
}
CustomType
[1]
Definition
src_corelib_kernel_qproperty.cpp:55
CustomType::CustomType
CustomType(int val, int otherVal)
Definition
src_corelib_kernel_qproperty.cpp:57
MyClassClientExample::Client
[5]
Definition
src_corelib_kernel_qproperty.cpp:139
MyClassClientExample::MyClassPrivate
Definition
src_corelib_kernel_qproperty.cpp:142
MyClassClientExample::MyClassPrivate::hasClientsActualCalculation
bool hasClientsActualCalculation() const
Definition
src_corelib_kernel_qproperty.cpp:145
MyClassClientExample::MyClassPrivate::clients
QList< Client > clients
Definition
src_corelib_kernel_qproperty.cpp:144
MyClassClientExample::MyClass
Definition
src_corelib_kernel_qproperty.cpp:151
MyClassExample::MyClass
[0]
Definition
src_corelib_kernel_qproperty.cpp:12
MyClass
[5]
Definition
doc_src_properties.cpp:68
SomeClass::someMethod
void someMethod()
Definition
src_corelib_thread_qfuture.cpp:577
main
int main()
[open]
Definition
doc_src_objecttrees.cpp:7
MyClassClientExample
[4]
Definition
src_corelib_kernel_qproperty.cpp:137
MyClassExample
Definition
src_corelib_kernel_qproperty.cpp:9
usage_QBindable
void usage_QBindable()
Definition
src_corelib_kernel_qproperty.cpp:79
Foo
[54]
Definition
src_corelib_global_qglobal.cpp:554
qtbase
src
corelib
doc
snippets
code
src_corelib_kernel_qproperty.cpp
Generated on
for Qt by
1.18.0