En esta página

QBindable Class

template <typename T> class QBindable

QBindable es una clase envolventora para propiedades con enlace habilitado. Permite realizar operaciones con seguridad de tipos, al tiempo que abstrae las diferencias entre las distintas clases de propiedades. Más...

Encabezado: #include <QBindable>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
Heredado de: 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 es una clase de plantilla en la que el parámetro de plantilla T especifica el tipo del valor de la propiedad.

QBindable<T> ayuda a integrar el « Q_PROPERTY » tradicional de Qt con propiedades que admiten enlace. Si una propiedad está respaldada por un ` QProperty`, ` QObjectBindableProperty ` o ` QObjectComputedProperty`, se 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 los enlaces de la propiedad, sin necesidad de conocer el tipo exacto de propiedad habilitada para enlaces que se utiliza.

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

QMetaProperty::isBindable QProperty QObjectBindableProperty QObjectComputedPropertyy «Propiedades vinculables de Qt».

Documentación de las funciones miembro

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

Véase 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)

Crea un QBindable para la propiedad Q_PROPERTY property en obj. La propiedad debe tener una señal «notify», pero no es necesario que incluy BINDABLE en su definición Q_PROPERTY, por lo que incluso los Q_PROPERTYque no admiten enlaces pueden enlazarse o utilizarse en expresiones de enlace. Debes utilizar QBindable::value() en las expresiones de enlace en lugar de la función habitual de la propiedad READ (o MEMBER) para habilitar el seguimiento de dependencias si la propiedad no es BINDABLE. Al realizar un enlace mediante 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 enlace. Este constructor no debe utilizarse para implementar ` BINDABLE ` para un ` Q_PROPERTY`, ya que el ` Q_PROPERTY ` resultante no admitirá el seguimiento de dependencias. Para crear una propiedad que se pueda utilizar directamente sin pasar por 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.

Véase también « QProperty », « QObjectBindableProperty » y «Propiedades Bindable de Qt».

QPropertyBinding<T> QBindable::binding() const

Devuelve el enlace actualmente establecido de la propiedad subyacente. Si la propiedad no tiene ningún enlace, el objeto ` QPropertyBinding<T> ` devuelto no será válido.

Véase también « setBinding » y « hasBinding ».

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

Crea un enlace cuyo valor corresponde al de la propiedad subyacente, utilizando una fuente especificada location.

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

Establece el enlace de la propiedad subyacente a binding. No realiza ninguna acción si QBindable es de solo lectura o no es válido.

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

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

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

Esta es una función sobrecargada.

void QBindable::setValue(const T &value)

Establece el valor de la propiedad subyacente en value. Esto elimina cualquier enlace que tenga establecido actualmente. Esta función no tiene ningún efecto si el QBindable es de solo lectura o no es válido.

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

QPropertyBinding<T> QBindable::takeBinding()

Elimina el enlace actualmente establecido de la propiedad subyacente y devuelve dicha propiedad. Si la propiedad no tiene ningún enlace, el objeto ` QPropertyBinding<T> ` devuelto no será válido.

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

T QBindable::value() const

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

Véanse 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.