QBindable Class

template <typename T> class QBindable

QBindable is a wrapper class around binding-enabled properties. It allows type-safe operations while abstracting the differences between the various property classes away. More...

Header: #include <QBindable>
CMake: find_package(Qt6 COMPONENTS Core REQUIRED) target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
Inherits: QUntypedBindable

Public Functions

QPropertyBinding<T> makeBinding(const QPropertyBindingSourceLocation &location = QT_PROPERTY_DEFAULT_BINDING_LOCATION)
QPropertyBinding<T> setBinding(const QPropertyBinding<T> &binding)
QPropertyBinding<T> setBinding(Functor f)
QPropertyBinding<T> takeBinding()
T value() const

Detailed Description

QBindable<T> helps to integrate Qt's traditional Q_PROPERTY with binding-enabled properties. If a property is backed by a QProperty, QObjectBindableProperty or QObjectComputedProperty, you can add BINDABLE bindablePropertyName to the Q_PROPERTY declaration, where bindablePropertyName is a function returning an instance of QBindable constructed from the QProperty. The returned QBindable allows users of the property to set and query bindings of the property, without having to know the exact kind of binding-enabled property used.

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; }
    QBindable<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)
};
MyClass *myObject;
QBindable<int> bindableX = myObject->bindableX();
qDebug() << bindableX.hasBinding(); // prints false
QProperty<int> y {42};
bindableX.setBinding([&](){ return 2*y.value(); });
qDebug() << bindableX.hasBinding() << myObject->x(); // prints true 84

See also QMetaProperty::isBindable, template, <typename, T>, QProperty<T>, and QObjectBindableProperty.

Member Function Documentation

QPropertyBinding<T> QBindable::makeBinding(const QPropertyBindingSourceLocation &location = QT_PROPERTY_DEFAULT_BINDING_LOCATION)

Constructs a binding evaluating to the underlying property's value.

QPropertyBinding<T> QBindable::setBinding(const QPropertyBinding<T> &binding)

Sets the underlying property's binding to binding. Does nothing if the QBindable is read-only or invalid.

See also binding, QPropertyBinding<T>::isValid(), isReadOnly(), and isValid().

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

This is an overloaded function.

Creates a QPropertyBinding<T> from f, and sets it as the underlying property's binding.

QPropertyBinding<T> QBindable::takeBinding()

Removes the currently set binding of the underlying property and returns it. If the property does not have a binding, the returned QPropertyBinding<T> will be invalid.

See also setBinding, getBinding, QPropertyBinding<T>::isValid(), and hasBinding.

T QBindable::value() const

Returns the underlying property's current value. If the QBindable is invalid, a default constructed T is returned.

See also isValid().

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