QObjectBindableProperty Class

template <typename Class, typename T, auto Offset, auto Signal> class QObjectBindableProperty

The QObjectBindableProperty class is a template class that enables automatic property bindings for property data stored in QObject derived classes. More...

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

Public Functions

QObjectBindableProperty(Class *owner, QPropertyBinding<T> &&binding)
QObjectBindableProperty(Class *owner, const QPropertyBinding<T> &binding)
QObjectBindableProperty(Functor &&f)
QObjectBindableProperty(T &&initialValue)
QObjectBindableProperty(const T &initialValue)
QObjectBindableProperty()
~QObjectBindableProperty()
QPropertyBinding<T> binding() const
bool hasBinding() const
QPropertyChangeHandler<Functor> onValueChanged(Functor f)
QPropertyBinding<T> setBinding(const QPropertyBinding<T> &newBinding)
bool setBinding(const QUntypedPropertyBinding &newBinding)
QPropertyBinding<T> setBinding(Functor f)
void setValue(QObjectBindableProperty::parameter_type newValue)
void setValue(QObjectBindableProperty::rvalue_ref newValue)
QPropertyChangeHandler<Functor> subscribe(Functor f)
QPropertyBinding<T> takeBinding()
QObjectBindableProperty::parameter_type value() const

Detailed Description

QObjectBindableProperty is a generic container that holds an instance of T and behaves mostly like QProperty. The extra template parameters are used to identify the surrounding class and a member function of that class. The member function will be called whenever the value held by the property changes.

You can use QObjectBindableProperty to add binding support to code that uses Q_PROPERTY. The getter and setter methods are easy to adapt for accessing a QObjectBindableProperty rather than the plain value. In order to invoke the change signal on property changes, use QObjectBindableProperty and pass the change signal as a callback.

QObjectBindableProperty is usually not used directly, instead an instance of it is created by using the Q_BINDABLE_PROPERTY_DATA macro.

Use the Q_BINDABLE_PROPERTY macro in the class declaration to declare the property as bindable.

class MyClass : public QObject
{
    \Q_OBJECT
    Q_PROPERTY(int x READ x WRITE setX NOTIFY xChanged BINDABLE bindableX)
public:
    int x() const { return xProp; }
    void setX(int x) { xProp = x; }
    Bindable<int> bindableX() { return QBindable<int>(&xProp); }

signals:
    void xChanged();

private:
    // Declare the instance of the bindable property data.
    Q_OBJECT_BINDABLE_PROPERTY(MyClass, int, xProp, &MyClass::xChanged)
};

If the property does not need a changed notification, you can leave out the "NOFITY xChanged" in the Q_PROPERTY macro as well as the last argument of the Q_OBJECT_BINDABLE_PROPERTY macro.

Member Function Documentation

void QObjectBindableProperty::setValue(QObjectBindableProperty::parameter_type newValue)

void QObjectBindableProperty::setValue(QObjectBindableProperty::rvalue_ref newValue)

Assigns newValue to this property and removes the property's associated binding, if present. If the property value changes as a result, calls the Callback function on owner.

[default] QObjectBindableProperty::QObjectBindableProperty(Class *owner, QPropertyBinding<T> &&binding)

Constructs a property that is tied to the provided binding expression. The first time the property value is read, the binding is evaluated. Whenever a dependency of the binding changes, the binding will be re-evaluated the next time the value of this property is read. When the property value changes owner is notified via the Callback function.

[default] QObjectBindableProperty::QObjectBindableProperty(Class *owner, const QPropertyBinding<T> &binding)

Constructs a property that is tied to the provided binding expression. The first time the property value is read, the binding is evaluated. Whenever a dependency of the binding changes, the binding will be re-evaluated the next time the value of this property is read. When the property value changes owner is notified via the Callback function.

template <typename Functor> QObjectBindableProperty::QObjectBindableProperty(Functor &&f)

Constructs a property that is tied to the provided binding expression f. The first time the property value is read, the binding is evaluated. Whenever a dependency of the binding changes, the binding will be re-evaluated the next time the value of this property is read.

QObjectBindableProperty::QObjectBindableProperty(T &&initialValue)

Move-Constructs a property with the provided initialValue.

QObjectBindableProperty::QObjectBindableProperty(const T &initialValue)

Constructs a property with the provided initialValue.

QObjectBindableProperty::QObjectBindableProperty()

Constructs a property with a default constructed instance of T.

[default] QObjectBindableProperty::~QObjectBindableProperty()

Destroys the property.

QPropertyBinding<T> QObjectBindableProperty::binding() const

Returns the binding expression that is associated with this property. A default constructed QPropertyBinding<T> will be returned if no such association exists.

See also setBinding().

bool QObjectBindableProperty::hasBinding() const

Returns true if the property is associated with a binding; false otherwise.

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

Registers the given functor f as a callback that shall be called whenever the value of the property changes.

The callback f is expected to be a type that has a plain call operator () without any parameters. This means that you can provide a C++ lambda expression, an std::function or even a custom struct with a call operator.

The returned property change handler object keeps track of the registration. When it goes out of scope, the callback is de-registered.

QPropertyBinding<T> QObjectBindableProperty::setBinding(const QPropertyBinding<T> &newBinding)

Associates the value of this property with the provided newBinding expression and returns the previously associated binding. The first time the property value is read, the binding is evaluated. Whenever a dependency of the binding changes, the binding will be re-evaluated the next time the value of this property is read. When the property value changes, the owner is notified via the Callback function.

See also binding().

bool QObjectBindableProperty::setBinding(const QUntypedPropertyBinding &newBinding)

This is an overloaded function.

Associates the value of this property with the provided newBinding expression. The first time the property value is read, the binding is evaluated. Whenever a dependency of the binding changes, the binding will be re-evaluated the next time the value of this property is read.

Returns true if the type of this property is the same as the type the binding function returns; false otherwise.

template <typename Functor> QPropertyBinding<T> QObjectBindableProperty::setBinding(Functor f)

This is an overloaded function.

Associates the value of this property with the provided functor f and returns the previously associated binding. The first time the property value is read, the binding is evaluated by invoking the call operator () of f. Whenever a dependency of the binding changes, the binding will be re-evaluated the next time the value of this property is read. When the property value changes, the owner is notified via the Callback function.

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

Subscribes the given functor f as a callback that is called immediately and whenever the value of the property changes in the future.

The callback f is expected to be a type that has a plain call operator () without any parameters. This means that you can provide a C++ lambda expression, an std::function or even a custom struct with a call operator.

The returned property change handler object keeps track of the subscription. When it goes out of scope, the callback is unsubscribed.

QPropertyBinding<T> QObjectBindableProperty::takeBinding()

Disassociates the binding expression from this property and returns it. After calling this function, the value of the property will only change if you assign a new value to it, or when a new binding is set.

QObjectBindableProperty::parameter_type QObjectBindableProperty::value() const

Returns the value of the property. This may evaluate a binding expression that is tied to this property, before returning the value.

See also setValue().

© The Qt Company Ltd
Licensed under the GNU Free Documentation License, Version 1.3.
https://doc.qt.io/qt-6.0/qobjectbindableproperty.html