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> 有助于将 Qt 传统的Q_PROPERTY 与支持绑定的属性整合在一起。如果一个属性由QProperty 、QObjectBindableProperty 或QObjectComputedProperty 支持,则可以在Q_PROPERTY 声明中添加BINDABLE
bindablePropertyName,其中 bindablePropertyName 是一个函数,返回由QProperty 构建的 QBindable 实例。返回的 QBindable 允许属性用户设置和查询属性的绑定,而无需知道所用绑定启用属性的确切类型。
类MyClass :公有QObject { 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); }信号:voidxChanged();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
另请参阅 QMetaProperty::isBindable,QProperty,QObjectBindableProperty,QObjectComputedProperty 和Qt Bindable Properties。
成员函数文档
[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。该属性必须有一个通知信号,但不需要在其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 Bindable Properties。
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
。
© 2025 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.