QObjectBindableProperty Class
template <typename Class, typename T, auto Offset, auto Signal> 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 |
| 等価性 | 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 はオプションで、プロパティ値が変更された際に発火させる通知シグナルを指定します。
`QObjectBindableProperty`は、`T`のインスタンスを保持する汎用コンテナであり、その挙動は主にQProperty と同様です。これは、Qtのバインド可能プロパティを実装するクラスの一つです。QProperty とは異なり、管理用データ構造を囲むQObject 内に格納します。追加のテンプレートパラメータは、囲むクラスと、変更ハンドラとして機能するそのクラスのメンバ関数を識別するために使用されます。
Q_PROPERTY を使用するコードにバインディング機能を追加するには、QObjectBindableProperty を使用できます。ゲッターおよびセッターメソッドは、「バインダブルプロパティのゲッターとセッター」に記載されているルールに従って、慎重に修正する必要があります。
プロパティの変更時に変更シグナルを呼び出すには、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 マクロを使用できます。このマクロは、初期化用の値をパラメータの1つとして受け取ります。
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)変更ハンドラは、オプションでプロパティと同じ型の引数を1つ受け取ることができます。その場合、プロパティの新しい値が引数として渡されます。それ以外の場合は、引数を受け取ってはいけません。
プロパティに変更通知が必要ない場合は、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++ のラムダ式、`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++ のラムダ式、`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++ のラムダ式、`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 が指定された場合、プロパティが変更済み(dirty)とマークされたときにこのシグナルが発火します。
このマクロは Qt 6.0 で導入されました。
QtのプロパティシステムおよびQtのバインド可能プロパティも参照してください 。
[since 6.0] Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(containingClass, type, name, initialvalue, signal)
containingClass 内で、型type 、名前name のQObjectBindableProperty を宣言し、initialvalue で初期化します。オプション引数signal が指定された場合、プロパティが変更済み(dirty)とマークされたときにこのシグナルが発信されます。
このマクロは 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.