本页内容

QObjectBindableProperty Class

template <typename Class, typename T, auto Offset, auto Signal> class QObjectBindableProperty

QObjectBindableProperty 类是一个模板类,它能够为存储在QObject 派生类中的属性数据实现自动属性绑定。更多内容...

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

QObjectBindableProperty 比较

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

公共函数

QObjectBindableProperty()
QObjectBindableProperty(Functor &&f)
QObjectBindableProperty(T &&initialValue)
QObjectBindableProperty(const T &initialValue)
QObjectBindableProperty(Class *owner, QPropertyBinding<T> &&binding)
QObjectBindableProperty(Class *owner, const QPropertyBinding<T> &binding)
~QObjectBindableProperty()
QPropertyNotifier addNotifier(Functor f)
QPropertyBinding<T> binding() const
bool hasBinding() const
void notify()
QPropertyChangeHandler<Functor> onValueChanged(Functor f)
QPropertyBinding<T> setBinding(const QPropertyBinding<T> &newBinding)
QPropertyBinding<T> setBinding(Functor f)
bool setBinding(const QUntypedPropertyBinding &newBinding)
void setValue(QObjectBindableProperty<Class, T, Offset, Signal>::parameter_type newValue)
void setValue(QObjectBindableProperty<Class, T, Offset, Signal>::rvalue_ref newValue)
QPropertyChangeHandler<Functor> subscribe(Functor f)
QPropertyBinding<T> takeBinding()
QObjectBindableProperty<Class, T, Offset, Signal>::parameter_type value() const

宏

(since 6.0) Q_OBJECT_BINDABLE_PROPERTY(containingClass, type, name, signal)
(since 6.0) Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(containingClass, type, name, initialvalue, signal)

详细说明

QObjectBindableProperty 是一个模板类,其中模板参数Class 指定拥有该属性的QObject 派生类,T 指定属性值的类型,Offset 指定属性在类中的位置,而Signal 可选地指定在属性值发生变化时要发出的 notify 信号。

QObjectBindableProperty 是一个通用容器,用于持有 T 的实例,其行为与QProperty 大致相同。它是实现Qt 可绑定属性的类之一。与QProperty 不同,它将管理数据结构存储在外部的QObject 中。额外的模板参数用于标识外部类以及该类中充当变化处理程序的成员函数。

您可以使用 QObjectBindableProperty 为使用Q_PROPERTY 的代码添加绑定支持。必须根据《可绑定属性的获取器和设置器》中描述的规则,仔细调整获取器和设置器方法。

若要在属性发生变化时触发变更信号,请使用 QObjectBindableProperty 并将变更信号作为回调函数传入。

下面给出一个简单的示例。

#include <QObject>
#include <QProperty>
#include <QDebug>

classFoo :publicQObject
{
    Q_OBJECT
    Q_PROPERTY(intmyVal READ myVal WRITE setMyVal BINDABLE bindableMyVal)
public:
    intmyVal() {returnmyValMember.value(); }
   voidsetMyVal(intnewvalue) { myValMember=newvalue; }
    QBindable<int>bindableMyVal() {return &myValMember; }
signals:
    voidmyValChanged();

private:
    Q_OBJECT_BINDABLE_PROPERTY(Foo, int,myValMember, &Foo::myValChanged);
};

intmain()
{
    booldebugout(true);// 启用调试日志
    Foo myfoo;
    QProperty<int>prop(42);
    QObject::connect(&myfoo, &Foo::myValChanged, [&]() {
        if(debugout)
            qDebug() << myfoo.myVal();
    });
    myfoo.bindableMyVal().setBinding([&]() {returnprop.value(); });// 输出“42”

    prop= 5;// 输出“5”
    debugout= false;
    prop= 6;// 不输出任何内容
    debugout= true;
    prop= 7;// 输出 "7"
}

#include "main.moc"

通常不会直接使用 QObjectBindableProperty,而是通过Q_OBJECT_BINDABLE_PROPERTY 宏 来创建其实例。

在类声明中使用Q_OBJECT_BINDABLE_PROPERTY 宏,将该属性声明为可绑定属性。

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:
    // Declare the instance of the bindable property data.
    Q_OBJECT_BINDABLE_PROPERTY(MyClass, int, xProp, &MyClass::xChanged)
};

如果需要使用非默认值直接初始化该属性,可以使用Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS 宏。该宏将初始化值作为其参数之一。

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:
    // Declare the instance of int bindable property data and
    // initialize it with the value 5.
    // This is similar to declaring
    // int xProp = 5;
    // without using the new QObjectBindableProperty class.
    Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(MyClass, int, xProp, 5, &MyClass::xChanged)
};

Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS 不直接支持多个参数。如果您的属性在初始化时需要多个参数,请显式调用相应的构造函数。

class CustomType
{
public:
    CustomType(int val, int otherVal) : value(val), anotherValue(otherVal) { }

private:
    int value = 0;
    int anotherValue = 0;
};

// later when using CustomType as a property
Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(SomeClass, CustomType, xProp, CustomType(5, 10),
                                    &MyClass::xChanged)

变更处理程序可选地接受一个与属性类型相同的参数,在这种情况下,该参数将接收属性的新值。否则,它不应接受任何参数。

如果该属性不需要变更通知,您可以省略Q_PROPERTY 宏中的“NOTIFY xChanged”,以及Q_OBJECT_BINDABLE_PROPERTY 和Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS 宏的最后一个参数。

另请参阅 Q_OBJECT_BINDABLE_PROPERTY 、Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS 、QProperty 、QObjectComputedProperty 、Qt 的属性系统以及Qt 可绑定属性。

成员函数文档

[default] QObjectBindableProperty::QObjectBindableProperty()

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

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

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

[explicit] QObjectBindableProperty::QObjectBindableProperty(T &&initialValue)

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

[explicit] QObjectBindableProperty::QObjectBindableProperty(const T &initialValue)

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

[implicit] QObjectBindableProperty::QObjectBindableProperty(Class *owner, QPropertyBinding<T> &&binding)

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

当属性值发生变化时,系统将通过回调函数通知 `owner `。

[implicit] QObjectBindableProperty::QObjectBindableProperty(Class *owner, const QPropertyBinding<T> &binding)

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

当属性值发生变化时,系统将通过回调函数通知 `owner `。

[implicit] QObjectBindableProperty::~QObjectBindableProperty()

破坏该财产。

template <typename Functor> QPropertyNotifier QObjectBindableProperty::addNotifier(Functor f)

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

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

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

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

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

QPropertyBinding<T> QObjectBindableProperty::binding() const

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

另请参阅 setBinding()。

bool QObjectBindableProperty::hasBinding() const

如果该属性与某个绑定相关联,则返回 true;否则返回 false。

void QObjectBindableProperty::notify()

通过编程方式通知属性发生变化。任何依赖该属性的绑定都会收到通知;如果该属性设有信号,则会触发该信号。

结合 setValueBypassingBindings 使用时,这有助于延迟发送变更信号,直到类不变式恢复为止。

注意:如果 该属性具有绑定(即 `hasBinding()` 返回 true),则在调用 `notify()` 时不会重新评估该绑定。任何依赖于该属性的绑定仍将照常重新评估。

另请参阅 ` Qt::beginPropertyUpdateGroup()` 和 `setValueBypassingBindings()`。

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

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

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

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

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

将该属性的值与提供的newBinding 表达式关联,并返回先前关联的绑定。该属性的值将设置为评估新绑定后的结果。每当绑定的依赖项发生变化时,绑定将被重新评估,属性的值也会相应更新。 当属性值发生变化时,将通过回调函数通知所有者。

另请参阅 binding()。

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

将该属性的值与提供的函子f 关联,并返回先前关联的绑定。该属性的值将设置为通过调用f 的调用运算符() 来求值新绑定所得的结果。每当绑定的依赖项发生变化时,绑定都会被重新求值,属性的值也会相应地更新。

当属性值发生变化时,系统会通过回调函数通知所有者。

这是一个重载函数。

另请参阅 《构建属性绑定》。

bool QObjectBindableProperty::setBinding(const QUntypedPropertyBinding &newBinding)

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

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

这是一个重载函数。

void QObjectBindableProperty::setValue(QObjectBindableProperty<Class, T, Offset, Signal>::parameter_type newValue)

void QObjectBindableProperty::setValue(QObjectBindableProperty<Class, T, Offset, Signal>::rvalue_ref newValue)

将newValue 赋值给该属性,并移除该属性关联的绑定(如有)。如果因此导致属性值发生变化,则调用owner 上的Callback函数。

template <typename Functor> QPropertyChangeHandler<Functor> QObjectBindableProperty::subscribe(Functor f)

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

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

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

QPropertyBinding<T> QObjectBindableProperty::takeBinding()

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

QObjectBindableProperty<Class, T, Offset, Signal>::parameter_type QObjectBindableProperty::value() const

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

另请参阅 setValue()。

宏文档

[since 6.0] Q_OBJECT_BINDABLE_PROPERTY(containingClass, type, name, signal)

在containingClass 中声明一个类型为type 、名称为name 的QObjectBindableProperty 。如果提供了可选参数signal ,则当该属性被标记为“已修改”时,将发出此信号。

该宏在 Qt 6.0 中引入。

另请参阅 Qt 的属性系统和 Qt 可绑定属性。

[since 6.0] Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(containingClass, type, name, initialvalue, signal)

在containingClass 中声明一个类型为type 的QObjectBindableProperty ,名称为name ,初始化值为initialvalue 。如果提供了可选参数signal ,则当该属性被标记为“已修改”时,将发出此信号。

该宏在 Qt 6.0 中引入。

另请参阅 Qt 的属性系统和 Qt 可绑定属性。

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