QBindable 類

template <typename T> class QBindable

QBindable 是圍繞啓用綁定特性的包裹器類。它允許類型安全操作,同時抽象齣各種特性類之間的差異。 更多...

頭: #include <QBindable>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
繼承: QUntypedBindable

公共函數

(從 6.5 起) QBindable (QObject * obj , const QMetaProperty & property )
(從 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> helps to integrate Qt's traditional Q_PROPERTY with binding-enabled properties. If a property is backed by a QProperty , QObjectBindableProperty or QObjectComputedProperty , you can add BINDABLE bindablePropertyName to the Q_PROPERTY declaration, where bindablePropertyName is a function returning an instance of QBindable constructed from the QProperty . The returned QBindable allows users of the property to set and query bindings of the property, without having to know the exact kind of binding-enabled property used.

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)
};
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 )

Constructs a QBindable for the Q_PROPERTY property on obj . The property must have a notify signal but does not need to have BINDABLE in its Q_PROPERTY definition, so even binding unaware Q_PROPERTY s can be bound or used in binding expressions. You must use QBindable::value() in binding expressions instead of the normal property READ function (or MEMBER ) to enable dependency tracking if the property is not BINDABLE . When binding using a lambda, you may prefer to capture the QBindable by value to avoid the cost of calling this constructor in the binding expression. This constructor should not be used to implement BINDABLE 對於 Q_PROPERTY , as the resulting Q_PROPERTY will not support dependency tracking. To make a property that is usable directly without reading through a QBindable use QProperty or 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

Returns the currently set binding of the underlying property. If the property does not have a binding, the returned QPropertyBinding<T> will be invalid.

另請參閱 setBinding and hasBinding .

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

Constructs a binding evaluating to the underlying property's value, using a specified source location .

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

Sets the underlying property's binding to binding . Does nothing if the QBindable 是隻讀或無效。

另請參閱 binding , isReadOnly (),和 isValid ().

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

創建 QPropertyBinding<T> from f , and sets it as the underlying property's binding.

這是重載函數。

void QBindable:: setValue (const T & value )

Sets the underlying property's value to value . This removes any currenltly set binding from it. This function has no effect if the QBindable 是隻讀或無效。

另請參閱 value (), isValid (), isReadOnly (),和 setBinding ().

QPropertyBinding < T > QBindable:: takeBinding ()

Removes the currently set binding of the underlying property and returns it. If the property does not have a binding, the returned QPropertyBinding<T> will be invalid.

另請參閱 binding , setBinding ,和 hasBinding .

T QBindable:: value () const

Returns the underlying property's current value. If the QBindable is invalid, a default constructed T 被返迴。

另請參閱 setValue () 和 isValid ().