このページでは

QBindable Class

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

パブリック関数

(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 でない場合、依存関係の追跡を有効にするには、バインディング式内で通常のプロパティのREAD 関数(またはMEMBER )の代わりにQBindable::value() を使用する必要があります。 ラムダ式を使用してバインディングを行う場合、バインディング式内でこのコンストラクタを呼び出す際のオーバーヘッドを避けるために、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 が返されます。

setValue() およびisValid()も参照してください 。

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