QObjectBindableProperty Class
template <typename Class, typename T, auto Offset, auto Signal = nullptr> class QObjectBindablePropertyQObjectBindableProperty クラスは、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(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 は T のインスタンスを保持する汎用コンテナで、QProperty のように動作します。Qt Bindable Properties を実装するクラスの1つです。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(); });// prints "42"prop= 5;// prints "5"debugout= false; prop= 6;// prints nothingdebugout= true; prop= 7;// prints "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(MyClass, CustomType xProp, CustomType(5, 10), &MyClass::xChanged)
変更ハンドラはオプションで、プロパティと同じ型の引数を1つ受け取ることができます。そうでない場合は、引数を取りません。
プロパティが変更通知を必要としない場合、Q_OBJECT_BINDABLE_PROPERTY とQ_OBJECT_BINDABLE_PROPERTY_WITH_ARGS マクロの最後の引数と同様に、Q_PROPERTY マクロの "NOTIFY xChanged" を省略することができます。
Q_OBJECT_BINDABLE_PROPERTY,Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS,QProperty,QObjectComputedProperty,Qt's Property System,Qt Bindable Propertiesも参照してください 。
メンバ関数のドキュメント
void QObjectBindableProperty::setValue(QObjectBindableProperty<Class, T, Offset, Signal>::parameter_type newValue)
void QObjectBindableProperty::setValue(QObjectBindableProperty<Class, T, Offset, Signal>::rvalue_ref newValue)
このプロパティにnewValue を割り当て、プロパティの関連バインディングが存在する場合はそれを削除します。その結果、プロパティの値が変更された場合、owner のコールバック関数を呼び出します。
QObjectBindableProperty::QObjectBindableProperty()
デフォルトで構築された T のインスタンスでプロパティを構築します。
[explicit]
template <typename Functor> QObjectBindableProperty::QObjectBindableProperty(Functor &&f)
提供されたバインディング式f に結び付けられたプロパティを構築します。プロパティの値は、新しいバインディングの評価結果に設定されます。バインディングの依存関係が変更されるたびに、バインディングが再評価され、それに応じてプロパティの値が更新されます。
[explicit]
QObjectBindableProperty::QObjectBindableProperty(T &&initialValue)
Move- 提供されたinitialValue でプロパティを構築します。
[explicit]
QObjectBindableProperty::QObjectBindableProperty(const T &initialValue)
提供されたinitialValue でプロパティを構築します。
[default]
QObjectBindableProperty::QObjectBindableProperty(Class *owner, QPropertyBinding<T> &&binding)
指定されたbinding 式に結び付けられたプロパティを構築します。プロパティの値は、新しいバインディングの評価結果に設定されます。バインディングの依存関係が変更されるたびに、バインディングが再評価され、それに応じてプロパティの値が更新されます。
プロパティの値が変更されると、owner にコールバック関数で通知されます。
[default]
QObjectBindableProperty::QObjectBindableProperty(Class *owner, const QPropertyBinding<T> &binding)
指定されたbinding 式に結び付けられたプロパティを構築します。プロパティの値は、新しいバインディングの評価結果に設定されます。バインディングの依存関係が変更されるたびに、バインディングが再評価され、それに応じてプロパティの値が更新されます。
プロパティの値が変更されると、owner にコールバック関数で通知されます。
[default]
QObjectBindableProperty::~QObjectBindableProperty()
財産を破壊する。
template <typename Functor> QPropertyNotifier QObjectBindableProperty::addNotifier(Functor f)
与えられたファンクタf を、プロパティの値が変更されるたびに呼び出されるコールバックとしてサブスクライブする。
コールバックf は、パラメータを持たないプレーンなコール演算子()
を持つ型であることが期待されます。つまり、C++のラムダ式やstd::関数、あるいは呼び出し演算子を持つカスタム構造体を指定することができます。
返されたプロパティ変更ハンドラオブジェクトは、サブスクリプションを追跡します。このオブジェクトがスコープ外に出ると、コールバックは購読を解除されます。
返されるオブジェクトはテンプレートではないので、このメソッドは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++のラムダ式やstd::関数、あるいはコール演算子を持つカスタム構造体を指定することができます。
返されたプロパティ変更ハンドラ・オブジェクトは、登録を追跡します。このオブジェクトがスコープ外に出ると、コールバックは登録解除されます。
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
を返します。
template <typename Functor> QPropertyChangeHandler<Functor> QObjectBindableProperty::subscribe(Functor f)
指定されたファンクタf を、プロパティの値が将来変更されるたびに即座に呼び出されるコールバックとしてサブスクライブします。値が変更されるたびに、ハンドラは即座に呼び出されるか、コンテキストに応じて遅延されます。
コールバックf は、パラメータを持たないプレーンなコール演算子()
を持つ型であることが期待されます。つまり、C++のラムダ式やstd::関数、あるいはコール演算子を持つカスタム構造体を指定することができます。
返されたプロパティ変更ハンドラオブジェクトは、サブスクリプションを追跡します。このオブジェクトがスコープ外に出ると、コールバックの購読が解除されます。
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)
type 型のcontainingClass 内にname という名前でQObjectBindableProperty を宣言する。オプションの引数signal が与えられると、プロパティがダーティとマークされたときにこのシグナルが発せられます。
このマクロは Qt 6.0 で導入されました。
Qt's Property SystemおよびQt Bindable Propertiesも参照してください 。
[since 6.0]
Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(containingClass, type, name, initialvalue, signal)
initialvalue に初期化される名前name を持つtype 型のcontainingClass の内部でQObjectBindableProperty を宣言します。オプションの引数signal が与えられると、プロパティがダーティとマークされたときにこのシグナルが発せられます。
このマクロは Qt 6.0 で導入されました。
Qtのプロパティシステムと Qtのバインダブルプロパティも参照してください 。
© 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.