QObjectComputedProperty Class

template <typename Class, typename T, auto Offset, auto Getter> class QObjectComputedProperty

The QObjectComputedProperty class is a template class to help port old properties to the bindable property system. More...

Header: #include <QObjectComputedProperty>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
Since: Qt 6.0
Inherits: QUntypedPropertyData

Public Types

Public Functions

QObjectComputedProperty()
QPropertyNotifier addNotifier(Functor f)
QtPrivate::QPropertyBindingData &bindingData() const
bool hasBinding() const
void notify()
QPropertyChangeHandler<Functor> onValueChanged(Functor f)
QPropertyChangeHandler<Functor> subscribe(Functor f)
QObjectComputedProperty::parameter_type value() const
QObjectComputedProperty::parameter_type operator QObjectComputedProperty::parameter_type() const
QObjectComputedProperty::parameter_type operator*() const
std::conditional_t<QTypeTraits::is_dereferenceable_v<T>, parameter_type, void> operator->() const

Macros

Q_OBJECT_COMPUTED_PROPERTY(containingClass, type, name, callback)

Detailed Description

QObjectComputedProperty is a read-only property which is recomputed on each read. It does not store the computed value. It is one of the Qt internal classes implementing Qt Bindable Properties. QObjectComputedProperty is usually not used directly, instead an instance of it is created by using the Q_OBJECT_COMPUTED_PROPERTY macro.

See the following example.

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)
};

The rules for getters in Bindable Property Getters and Setters also apply for QObjectComputedProperty. Especially, the getter should be trivial and only return the value of the QObjectComputedProperty object. The callback given to the QObjectComputedProperty should usually be a private method which is only called by the QObjectComputedProperty.

No setter is required or allowed, as QObjectComputedProperty is read-only.

To correctly participate in dependency handling, QObjectComputedProperty has to know when its value, the result of the callback given to it, might have changed. Whenever a bindable property used in the callback changes, this happens automatically. If the result of the callback might change because of a change in a value which is not a bindable property, it is the developer's responsibility to call markDirty on the QObjectComputedProperty object. This will inform dependent properties about the potential change.

Note that calling markDirty might trigger change handlers in dependent properties, which might in turn use the object the QObjectComputedProperty is a member of. So markDirty must not be called when in a transitional or invalid state.

QObjectComputedProperty is not suitable for use with a computation that depends on any input that might change without notice, such as the contents of a file.

See also Q_OBJECT_COMPUTED_PROPERTY, QProperty, QObjectBindableProperty, Qt's Property System, and Qt Bindable Properties.

Member Type Documentation

[alias] QObjectComputedProperty::parameter_type

[alias] QObjectComputedProperty::value_type

Member Function Documentation

QObjectComputedProperty::QObjectComputedProperty()

Default constructs an instance of QObjectComputedProperty.

template <typename Functor> QPropertyNotifier QObjectComputedProperty::addNotifier(Functor f)

QtPrivate::QPropertyBindingData &QObjectComputedProperty::bindingData() const

bool QObjectComputedProperty::hasBinding() const

void QObjectComputedProperty::notify()

template <typename Functor> QPropertyChangeHandler<Functor> QObjectComputedProperty::onValueChanged(Functor f)

template <typename Functor> QPropertyChangeHandler<Functor> QObjectComputedProperty::subscribe(Functor f)

QObjectComputedProperty::parameter_type QObjectComputedProperty::value() const

QObjectComputedProperty::parameter_type QObjectComputedProperty::operator QObjectComputedProperty::parameter_type() const

QObjectComputedProperty::parameter_type QObjectComputedProperty::operator*() const

std::conditional_t<QTypeTraits::is_dereferenceable_v<T>, parameter_type, void> QObjectComputedProperty::operator->() const

Macro Documentation

[since 6.0] Q_OBJECT_COMPUTED_PROPERTY(containingClass, type, name, callback)

Declares a QObjectComputedProperty inside containingClass of type type with name name. The argument callback specifies a GETTER function to be called when the property is evaluated.

This macro was introduced in Qt 6.0.

See also QObjectBindableProperty, Qt's Property System, and Qt Bindable Properties.

© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.