本页内容

QProperty Class

template <typename T> class QProperty

QProperty 类是一个模板类,用于实现自动属性绑定。更多内容...

头文件: #include <QProperty>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
自: Qt 6.0
继承自: QPropertyData

QProperty 比较

类别可比较类型
相等性QProperty
相等性T.

公共函数

QProperty()
QProperty(Functor &&f)
QProperty(T &&initialValue)
QProperty(const QPropertyBinding<T> &binding)
QProperty(const T &initialValue)
~QProperty()
(since 6.2) QPropertyNotifier addNotifier(Functor f)
QPropertyBinding<T> binding() const
QPropertyChangeHandler<Functor> onValueChanged(Functor f)
QPropertyBinding<T> setBinding(const QPropertyBinding<T> &newBinding)
(since 6.0) QPropertyBinding<T> setBinding(Functor f)
bool setBinding(const QUntypedPropertyBinding &newBinding)
void setValue(QProperty<T>::parameter_type newValue)
void setValue(QProperty<T>::rvalue_ref newValue)
(since 6.0) QPropertyChangeHandler<Functor> subscribe(Functor f)
QPropertyBinding<T> takeBinding()
QProperty<T>::parameter_type value() const
QProperty<T> &operator=(QProperty<T>::parameter_type newValue)
QProperty<T> &operator=(QProperty<T>::rvalue_ref newValue)
(since 6.2) void beginPropertyUpdateGroup()
(since 6.2) void endPropertyUpdateGroup()

详细说明

QProperty 是一个模板类,其中模板参数T 指定了属性值的类型。

QProperty<T> 是实现Qt 可绑定属性的类之一。它是一个容纳 T 实例的容器。您可以为其赋值,并可通过value() 函数或 T 的转换运算符读取该值。 您还可以将该属性绑定到一个动态计算值的表达式上,即绑定表达式。它以 C++ lambda 形式表示,可用于表达应用程序中不同属性之间的关系。

注意:对于 QML,必须在Q_PROPERTY 中使用 BINDABLE 关键字暴露QProperty 。这样,QML 引擎才会将其用作可绑定接口来设置属性绑定。进而,该绑定可通过常规 API 与 C++ 进行交互: QProperty<T>::onValueChanged、QProperty::takeBinding 和 QBindable::hasBinding。如果属性具有 BINDABLE 属性,引擎将利用 C++ 属性系统固有的变更跟踪机制来获取变更通知,而不会依赖信号的发出。

成员函数文档

[default] QProperty::QProperty()

创建一个包含 T 类型默认构造实例的属性。

[explicit] template <typename Functor> QProperty::QProperty(Functor &&f)

创建一个与给定的绑定表达式f 相关联的属性。该属性的值将设置为新绑定求值后的结果。每当绑定的依赖项发生变化时,该绑定都会被重新求值,属性的值也会相应地更新。

[explicit implicit] QProperty::QProperty(T &&initialValue)

Move-Constructs 根据提供的initialValue 构建一个属性。

[explicit] QProperty::QProperty(const QPropertyBinding<T> &binding)

创建一个与给定的binding 表达式绑定的属性。该属性的值将设置为新绑定求值后的结果。每当绑定的依赖项发生变化时,绑定都会被重新求值,属性的值也会相应地更新。

[explicit implicit] QProperty::QProperty(const T &initialValue)

根据提供的initialValue 构建一个属性。

[default] QProperty::~QProperty()

破坏该财产。

[since 6.2] template <typename Functor> QPropertyNotifier QProperty::addNotifier(Functor f)

将给定的函子f 注册为回调函数,每当属性值发生变化时都会调用该回调。

回调函数f 应为一种具有无参调用运算符() 的类型。这意味着您可以提供 C++ lambda 表达式、std::function,甚至是带有调用运算符的自定义结构体。

返回的属性变更处理程序对象会跟踪该订阅。当该对象超出作用域时,回调函数的订阅将被取消。

在某些情况下,该方法比 `onValueChanged()` 更易于使用,因为返回的对象不是模板。因此,它可以更方便地进行存储,例如作为类中的成员。

该函数在 Qt 6.2 中引入。

另请参见 onValueChanged() 和subscribe()。

QPropertyBinding<T> QProperty::binding() const

返回与该属性关联的绑定表达式。如果不存在此类关联,则返回一个通过默认构造函数构造的 QPropertyBinding<T>。

另请参阅 setBinding()。

template <typename Functor> QPropertyChangeHandler<Functor> QProperty::onValueChanged(Functor f)

将给定的函子f 注册为回调函数,每当属性值发生变化时,该回调函数就会被调用。每次值发生变化时,根据上下文的不同,处理程序要么立即被调用,要么被延迟调用。

回调函数f 应为一种具有无参数的普通调用运算符() 的类型。这意味着您可以提供 C++ lambda 表达式、std::function,甚至是带有调用运算符的自定义结构体。

返回的属性变更处理程序对象会记录该注册信息。当该对象超出作用域时,回调函数将被注销。

QPropertyBinding<T> QProperty::setBinding(const QPropertyBinding<T> &newBinding)

将该属性的值与提供的newBinding 表达式关联,并返回先前关联的绑定。该属性的值将设置为新绑定求值后的结果。每当绑定的依赖项发生变化时,绑定都会被重新求值,该属性的值也会相应地更新。

另请参阅 binding()。

[since 6.0] template <typename Functor> QPropertyBinding<T> QProperty::setBinding(Functor f)

将该属性的值与给定的函子f 关联,并返回先前关联的绑定。该属性的值将设置为新绑定求值的结果。每当绑定的依赖项发生变化时,绑定都会被重新求值,属性的值也会相应地更新。

这是一个重载函数。

该函数在 Qt 6.0 中引入。

另请参阅 《制定属性绑定》。

bool QProperty::setBinding(const QUntypedPropertyBinding &newBinding)

将该属性的值与提供的newBinding 表达式关联起来。该属性的值将设置为新绑定求值后的结果。每当绑定的依赖项发生变化时,绑定都会被重新求值,该属性的值也会相应地更新。

如果该属性的类型与绑定函数返回的类型相同,则返回 true;否则返回 false。

这是一个重载函数。

void QProperty::setValue(QProperty<T>::parameter_type newValue)

void QProperty::setValue(QProperty<T>::rvalue_ref newValue)

将newValue 赋值给此属性,并删除该属性关联的绑定(如有)。

[since 6.0] template <typename Functor> QPropertyChangeHandler<Functor> QProperty::subscribe(Functor f)

将给定的 functorf 注册为回调函数,该回调会在属性值发生变化时立即被调用,并在未来属性值发生变化时再次被调用。每次值发生变化时,根据上下文的不同,处理程序要么立即被调用,要么被延迟调用。

回调函数f 应为可复制的类型,并具有不带任何参数的普通调用运算符 operator()。这意味着您可以提供 C++ lambda 表达式、std::function,甚至带有调用运算符的自定义结构体。

返回的属性变化处理程序对象会跟踪该订阅。当该对象超出作用域时,回调将被取消订阅。

该函数于 Qt 6.0 中引入。

QPropertyBinding<T> QProperty::takeBinding()

将绑定表达式与该属性解除绑定,并返回该属性。调用此函数后,该属性的值仅会在为其赋值或设置新的绑定时发生变化。

QProperty<T>::parameter_type QProperty::value() const

返回该属性的值。在返回该值之前,可能会先求值一个与该属性绑定的绑定表达式。

另请参阅 setValue()。

QProperty<T> &QProperty::operator=(QProperty<T>::parameter_type newValue)

QProperty<T> &QProperty::operator=(QProperty<T>::rvalue_ref newValue)

将newValue 赋值给该属性,并返回对该QProperty 的引用。

相关的非成员属性

[since 6.2] void beginPropertyUpdateGroup()

标记属性更新组的开始。在此组内,修改某个属性既不会立即更新任何依赖属性,也不会触发变更通知。这些操作将被推迟,直到通过调用endPropertyUpdateGroup 结束该组为止。

组可以嵌套。在这种情况下,延迟处理仅在最外层组结束之后才会终止。

注意: 只有在该组影响的所有属性值均已更新为新值后,才会发送变更 通知。这使得在需要更新多个属性时能够重新建立类的不变性,从而防止任何外部观察者察觉到不一致的状态。

该函数在 Qt 6.2 中引入。

另请参阅 Qt::endPropertyUpdateGroup 和QScopedPropertyUpdateGroup 。

[since 6.2] void endPropertyUpdateGroup()

结束一个属性更新组。如果最外层的组已被结束,则此时将执行延迟的绑定评估和通知。

警告:若在调用 `endPropertyUpdateGroup` 之前未先调用 `beginPropertyUpdateGroup `,将导致未定义行为。

该函数在 Qt 6.2 中引入。

另请参阅 Qt::beginPropertyUpdateGroup 和QScopedPropertyUpdateGroup 。

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