Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
8class MyClass : public QObject
9{
11 Q_PROPERTY(int x READ x WRITE setX NOTIFY xChanged BINDABLE bindableX)
12public:
13 int x() const { return xProp; }
14 void setX(int x) { xProp = x; }
15 QBindable<int> bindableX() { return QBindable<int>(&xProp); }
16
18 void xChanged();
19
20private:
21 // Declare the instance of the bindable property data.
22 Q_OBJECT_BINDABLE_PROPERTY(MyClass, int, xProp, &MyClass::xChanged)
23};
25
27class MyClass : public QObject
28{
30 Q_PROPERTY(int x READ x WRITE setX NOTIFY xChanged BINDABLE bindableX)
31public:
32 int x() const { return xProp; }
33 void setX(int x) { xProp = x; }
34 QBindable<int> bindableX() { return QBindable<int>(&xProp); }
35
37 void xChanged();
38
39private:
40 // Declare the instance of int bindable property data and
41 // initialize it with the value 5.
42 // This is similar to declaring
43 // int xProp = 5;
44 // without using the new QObjectBindableProperty class.
45 Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(MyClass, int, xProp, 5, &MyClass::xChanged)
46};
48
51{
52public:
53 CustomType(int val, int otherVal) : value(val), anotherValue(otherVal) { }
54
55private:
56 int value = 0;
57 int anotherValue = 0;
58};
59
60// later when using CustomType as a property
62 &MyClass::xChanged)
64
65void usage_QBindable() {
68 QBindable<int> bindableX = myObject->bindableX();
69 qDebug() << bindableX.hasBinding(); // prints false
70 QProperty<int> y {42};
71 bindableX.setBinding([&](){ return 2*y.value(); });
72 qDebug() << bindableX.hasBinding() << myObject->x(); // prints true 84
74}
75
77#include <QObject>
78#include <QProperty>
79#include <QDebug>
80
81class Foo : public QObject
82{
84 Q_PROPERTY(int myVal READ myVal WRITE setMyVal BINDABLE bindableMyVal)
85public:
86 int myVal() { return myValMember.value(); }
87 void setMyVal(int newvalue) { myValMember = newvalue; }
88 QBindable<int> bindableMyVal() { return &myValMember; }
91
92private:
93 Q_OBJECT_BINDABLE_PROPERTY(Foo, int, myValMember, &Foo::myValChanged);
94};
95
96int main()
97{
98 bool debugout(true); // enable debug log
99 Foo myfoo;
100 QProperty<int> prop(42);
101 QObject::connect(&myfoo, &Foo::myValChanged, [&]() {
102 if (debugout)
103 qDebug() << myfoo.myVal();
104 });
105 myfoo.bindableMyVal().setBinding([&]() { return prop.value(); }); // prints "42"
106
107 prop = 5; // prints "5"
108 debugout = false;
109 prop = 6; // prints nothing
110 debugout = true;
111 prop = 7; // prints "7"
112}
113
114#include "main.moc"
116
118class Client{};
119
121{
122public:
123 QList<Client> clients;
124 bool hasClientsActualCalculation() const { return clients.size() > 0; }
125 Q_OBJECT_COMPUTED_PROPERTY(MyClassPrivate, bool, hasClientsData,
127};
128
129class MyClass : public QObject
130{
132 Q_PROPERTY(bool hasClients READ hasClients STORED false BINDABLE bindableHasClients)
133public:
134 QBindable<bool> bindableHasClients()
135 {
136 return QBindable<bool>(&d_func()->hasClientsData);
137 }
138 bool hasClients() const
139 {
140 return d_func()->hasClientsData.value();
141 }
142 void addClient(const Client &c)
143 {
144 Q_D(MyClass);
145 d->clients.push_back(c);
146 // notify that the value could have changed
147 d->hasClientsData.notify();
148 }
149private:
150 Q_DECLARE_PRIVATE(MyClass)
151};
CustomType(int val, int otherVal)
void setMyVal(int newvalue)
void myValChanged()
QBindable< int > bindableMyVal()
void addClient(const Client &c)
QBindable< int > bindableX()
QBindable< bool > bindableHasClients()
void xChanged()
qsizetype size() const noexcept
Definition qlist.h:397
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define qDebug
[1]
Definition qlogging.h:164
GLint GLint GLint GLint GLint x
[0]
GLint y
const GLubyte * c
GLuint GLfloat * val
#define Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(...)
Definition qproperty.h:1266
#define Q_OBJECT_COMPUTED_PROPERTY(Class, Type, name, ...)
Definition qproperty.h:1355
#define Q_OBJECT_BINDABLE_PROPERTY(...)
Definition qproperty.h:1239
#define Q_PROPERTY(...)
#define Q_OBJECT
#define signals