QObjectComputedProperty docs: move example to snippet

This patch amends 89a4c8d40d.
It moves the code sample into a separate snippet file, which allows us
to use Q_OBJECT macro in it without complaints from moc.

Task-number: QTBUG-97656
Pick-to: 6.2
Change-Id: I368d8dd8c00dbbebd8a6bf3788be796c8ca4bce8
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
bb10
Ivan Solovev 2021-10-25 17:45:29 +02:00
parent da632baf34
commit 5f0e57ebc7
2 changed files with 38 additions and 36 deletions

View File

@ -160,3 +160,40 @@ int main()
#include "main.moc"
//! [4]
//! [5]
class Client{};
class MyClassPrivate : public QObjectPrivate
{
public:
QList<Client> clients;
bool hasClientsActualCalculation() const { return clients.size() > 0; }
Q_OBJECT_COMPUTED_PROPERTY(MyClassPrivate, bool, hasClientsData,
&MyClassPrivate::hasClientsActualCalculation)
};
class MyClass : public QObject
{
Q_OBJECT
Q_PROPERTY(bool hasClients READ hasClients STORED false BINDABLE bindableHasClients)
public:
QBindable<bool> bindableHasClients()
{
return QBindable<bool>(&d_func()->hasClientsData);
}
bool hasClients() const
{
return d_func()->hasClientsData.value();
}
void addClient(const Client &c)
{
Q_D(MyClass);
d->clients.push_back(c);
// notify that the value could have changed
d->hasClientsData.markDirty();
}
private:
Q_DECLARE_PRIVATE(MyClass)
};
//! [5]

View File

@ -1565,42 +1565,7 @@ QString QPropertyBindingError::description() const
See the following example.
\code
class Client{};
class MyClassPrivate : public QObjectPrivate
{
public:
QList<Client> clients;
bool hasClientsActualCalculation() const { return clients.size() > 0; }
Q_OBJECT_COMPUTED_PROPERTY(MyClassPrivate, bool, hasClientsData,
&MyClassPrivate::hasClientsActualCalculation)
};
class MyClass : public QObject
{
// add q-object macro here (confuses qdoc if we do it here)
Q_PROPERTY(bool hasClients READ hasClients STORED false BINDABLE bindableHasClients)
public:
QBindable<bool> bindableHasClients()
{
return QBindable<bool>(&d_func()->hasClientsData);
}
bool hasClients() const
{
return d_func()->hasClientsData.value();
}
void addClient(const Client &c)
{
Q_D(MyClass);
d->clients.push_back(c);
// notify that the value could have changed
d->hasClientsData.markDirty();
}
private:
Q_DECLARE_PRIVATE(MyClass)
};
\endcode
\snippet code/src_corelib_kernel_qproperty.cpp 5
The rules for getters in \l {Bindable Property Getters and Setters}
also apply for QObjectComputedProperty. Especially, the getter