En esta página

QBindable Class

template <typename T> class QBindable

QBindable es una clase que envuelve las propiedades vinculables. Permite realizar operaciones seguras mientras abstrae las diferencias entre las distintas clases de propiedades. Más...

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

Funciones públicas

(since 6.5) QBindable(QObject *obj, const QMetaProperty &property)
(since 6.5) QBindable(QObject *obj, const char *property)
QPropertyBinding<T> binding() const
QPropertyBinding<T> makeBinding(const QPropertyBindingSourceLocation &location = QT_PROPERTY_DEFAULT_BINDING_LOCATION) const
QPropertyBinding<T> setBinding(const QPropertyBinding<T> &binding)
QPropertyBinding<T> setBinding(Functor f)
void setValue(const T &value)
QPropertyBinding<T> takeBinding()
T value() const

Descripción detallada

QBindable<T> ayuda a integrar la tradicional Q_PROPERTY de Qt con propiedades con capacidad de vinculación. Si una propiedad está respaldada por un QProperty, QObjectBindableProperty o QObjectComputedProperty, puede añadir BINDABLE bindablePropertyName a la declaración Q_PROPERTY, donde bindablePropertyName es una función que devuelve una instancia de QBindable construida a partir de QProperty. El QBindable devuelto permite a los usuarios de la propiedad establecer y consultar vinculaciones de la propiedad, sin tener que conocer el tipo exacto de propiedad habilitada para vinculación utilizado.

clase 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: // Declara la instancia de los datos de la propiedad bindable.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

Ver también QMetaProperty::isBindable, QProperty, QObjectBindableProperty, QObjectComputedProperty, y Qt Bindable Properties.

Documentación de funciones miembro

[explicit, since 6.5] QBindable::QBindable(QObject *obj, const QMetaProperty &property)

Ver QBindable::QBindable(QObject *obj, const char *property)

Esta función se introdujo en Qt 6.5.

[explicit, since 6.5] QBindable::QBindable(QObject *obj, const char *property)

Construye un QBindable para Q_PROPERTY property en obj. La propiedad debe tener una señal de notificación, pero no es necesario que tenga BINDABLE en su definición de Q_PROPERTY, por lo que incluso las propiedades Q_PROPERTYsin vinculación pueden vincularse o utilizarse en expresiones de vinculación. Debe utilizar QBindable::value() en las expresiones de vinculación en lugar de la función normal de propiedad READ (o MEMBER) para permitir el seguimiento de dependencias si la propiedad no es BINDABLE. Cuando se vincula utilizando una lambda, es posible que prefiera capturar el QBindable por valor para evitar el coste de llamar a este constructor en la expresión de vinculación. Este constructor no debe utilizarse para implementar BINDABLE para un Q_PROPERTY, ya que el Q_PROPERTY resultante no soportará el seguimiento de dependencias. Para hacer una propiedad que sea utilizable directamente sin leer a través de un QBindable utilice QProperty o QObjectBindableProperty.

QProperty<QString> displayText;
QDateTimeEdit *dateTimeEdit = findDateTimeEdit();
QBindable<QDateTime> dateTimeBindable(dateTimeEdit, "dateTime");
displayText.setBinding([dateTimeBindable](){ return dateTimeBindable.value().toString(); });

Esta función se introdujo en Qt 6.5.

Ver también QProperty, QObjectBindableProperty, y Qt Bindable Properties.

QPropertyBinding<T> QBindable::binding() const

Devuelve la vinculación actual de la propiedad subyacente. Si la propiedad no tiene una vinculación, el valor devuelto QPropertyBinding<T> no será válido.

Véase también setBinding y hasBinding.

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

Construye un enlace que evalúa el valor de la propiedad subyacente, utilizando una fuente especificada location.

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

Establece el enlace de la propiedad subyacente en binding. No hace nada si QBindable es de sólo lectura o inválido.

Véase también binding, isReadOnly(), y isValid().

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

Crea un QPropertyBinding<T> a partir de f, y lo establece como enlace de la propiedad subyacente.

Se trata de una función sobrecargada.

void QBindable::setValue(const T &value)

Establece el valor de la propiedad subyacente en value. De este modo se elimina cualquier vinculación establecida actualmente. Esta función no tiene efecto si QBindable es de sólo lectura o inválida.

Véase también value(), isValid(), isReadOnly() y setBinding().

QPropertyBinding<T> QBindable::takeBinding()

Elimina la vinculación actualmente establecida de la propiedad subyacente y la devuelve. Si la propiedad no tiene una vinculación, la dirección QPropertyBinding<T> devuelta no será válida.

Véase también binding, setBinding, y hasBinding.

T QBindable::value() const

Devuelve el valor actual de la propiedad subyacente. Si el QBindable no es válido, se devuelve un T construido por defecto.

Véase también setValue() y isValid().

© 2026 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.