QBindable Class
template <typename T> class QBindableQBindable 是一个围绕支持绑定的属性的封装类。它可以在抽象出各种属性类之间差异的同时,确保操作的类型安全。更多内容...
| 标题: | #include <QBindable> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
| 继承自: | QUntypedBindable |
公共函数
(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 |
详细说明
QBindable 是一个模板类,其中模板参数T 指定了属性值的类型。
QBindable<T> 有助于将 Qt 的传统Q_PROPERTY 与支持绑定的属性进行集成。 如果一个属性由QProperty 、QObjectBindableProperty 或QObjectComputedProperty 支持,则可以在Q_PROPERTY 声明中添加BINDABLE bindablePropertyName,其中 bindablePropertyName 是一个函数,该函数返回一个由QProperty 构建的 QBindable 实例。返回的 QBindable 允许属性的用户设置和查询该属性的绑定,而无需了解所使用的支持绑定的属性的确切类型。
classMyClass :publicQObject
{
Q_OBJECT
Q_PROPERTY(intx READ x WRITE setX NOTIFY xChanged BINDABLE bindableX)
public:
intx()const{returnxProp; }
voidsetX(intx) { xProp=x; }
QBindable<int>bindableX() {returnQBindable<int>(&xProp); }
signals:
voidxChanged();
private:
// 声明可绑定属性的实例数据。
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 、QObjectComputedProperty 以及Qt 可绑定属性。
成员函数文档
[explicit, since 6.5] QBindable::QBindable(QObject *obj, const QMetaProperty &property)
参见QBindable::QBindable(QObject *obj, const char *property)
该函数在 Qt 6.5 中引入。
[explicit, since 6.5] QBindable::QBindable(QObject *obj, const char *property)
为obj 上的Q_PROPERTY property 构建一个 QBindable 对象。该属性必须具有 notify 信号,但在其Q_PROPERTY 定义中无需包含BINDABLE ,因此即使是未支持绑定的Q_PROPERTY也可以进行绑定或在绑定表达式中使用。如果该属性不是BINDABLE ,则必须在绑定表达式中使用QBindable::value() 代替常规的属性READ 函数(或MEMBER ),以启用依赖关系追踪。 在使用 lambda 进行绑定时,您可能更倾向于按值捕获 QBindable,以避免在绑定表达式中调用此构造函数带来的开销。不应使用此构造函数为Q_PROPERTY 实现BINDABLE ,因为生成的Q_PROPERTY 将不支持依赖项追踪。若要创建无需通过 QBindable 读取即可直接使用的属性,请使用QProperty 或QObjectBindableProperty 。
QProperty<QString> displayText;
QDateTimeEdit *dateTimeEdit = findDateTimeEdit();
QBindable<QDateTime> dateTimeBindable(dateTimeEdit, "dateTime");
displayText.setBinding([dateTimeBindable](){ return dateTimeBindable.value().toString(); });该函数于 Qt 6.5 中引入。
另请参阅 QProperty 、QObjectBindableProperty 以及Qt 可绑定属性。
QPropertyBinding<T> QBindable::binding() const
返回底层属性当前设置的绑定。如果该属性没有绑定,则返回的QPropertyBinding<T> 将无效。
另请参阅 setBinding 和hasBinding 。
QPropertyBinding<T> QBindable::makeBinding(const QPropertyBindingSourceLocation &location = QT_PROPERTY_DEFAULT_BINDING_LOCATION) const
使用指定的来源location 构建一个绑定,该绑定的计算结果即为底层属性的值。
QPropertyBinding<T> QBindable::setBinding(const QPropertyBinding<T> &binding)
将底层属性的绑定设置为binding 。如果QBindable 为只读或无效,则不执行任何操作。
另请参阅 binding 、isReadOnly() 和isValid()。
template <typename Functor> QPropertyBinding<T> QBindable::setBinding(Functor f)
根据 `f` 创建一个 `QPropertyBinding<T> `,并将其设置为底层属性的绑定。
这是一个重载函数。
void QBindable::setValue(const T &value)
将底层属性的值设置为value 。这将移除该属性当前设置的任何绑定。如果QBindable 为只读或无效,则此函数无效。
另请参阅 value()、isValid()、isReadOnly() 以及setBinding()。
QPropertyBinding<T> QBindable::takeBinding()
移除底层属性的当前绑定,并返回该属性。如果该属性没有绑定,则返回的QPropertyBinding<T> 将无效。
另请参阅 binding 、setBinding 和hasBinding 。
T QBindable::value() const
返回底层属性的当前值。如果QBindable 无效,则返回一个默认构造的T 。
© 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.