このページでは

QQuickAttachedPropertyPropagator Class

QQuickAttachedPropertyPropagator クラスは、アタッチドプロパティを伝播させる手段を提供します。詳細...

ヘッダー: #include <QQuickAttachedPropertyPropagator>
CMake: find_package(Qt6 REQUIRED COMPONENTS QuickControls2)
target_link_libraries(mytarget PRIVATE Qt6::QuickControls2)
qmake: QT += quickcontrols2
以下のように: Qt 6.5
継承元: QObject

パブリック関数

QQuickAttachedPropertyPropagator(QObject *parent = nullptr)
virtual ~QQuickAttachedPropertyPropagator()
QList<QQuickAttachedPropertyPropagator *> attachedChildren() const
QQuickAttachedPropertyPropagator *attachedParent() const

プロテクト関数

virtual void attachedParentChange(QQuickAttachedPropertyPropagator *newParent, QQuickAttachedPropertyPropagator *oldParent)
void initialize()

詳細な説明

QML では、オブジェクトにプロパティやシグナルハンドラをアタッチすることができます。「アタッチされたプロパティの提供」では、独自の C++ アタッチされた型を公開する方法についてさらに詳しく説明しています。

QQuickAttachedPropertyPropagatorは、font やpalette の伝播と同様に、親オブジェクトから子オブジェクトへアタッチされたプロパティを伝播するためのAPIを提供します。これは、items 、popups 、およびwindows を通じた伝播をサポートしています。

プロパティの伝播が重要でない場合は、代わりにC++またはQMLのシングルトンを使用することを検討してください。シングルトンはそのユースケースにより適しており、QObject を 1 つだけ必要とするため、より効率的です。

カスタムアタッチドプロパティを実装するには:

  1. アタッチドプロパティを公開するクラスを QQuickAttachedPropertyPropagator から派生させます。

    たとえば、MyStyle.theme アタッチドプロパティを実装するには、MyStyle クラスを次のように宣言します:

    class MYSTYLE_EXPORT MyStyle : public QQuickAttachedPropertyPropagator
  2. クラスのコンストラクタ内で `initialize()` を呼び出します:
    MyStyle::MyStyle(QObject *parent)
        : QQuickAttachedPropertyPropagator(parent)
        , m_theme(globalTheme)
    {
        // A static function could be called here that reads globalTheme from a
        // settings file once at startup. That value would override the global
        // value. This is similar to what the Imagine and Material styles do, for
        // example.
    
        initialize();
    }
  3. 必要に応じて、アタッチドプロパティの set/inherit/propagate/reset 関数を定義します。たとえば、theme というアタッチドプロパティを定義するには:
    MyStyle::Theme MyStyle::theme() const
    {
        return m_theme;
    }
    
    void MyStyle::setTheme(Theme theme)
    {
        // When this function is called, we know that the user has explicitly
        // set a theme on this attached object. We set this to true even if
        // the effective theme didn't change, because it's important that
        // the user's specified value is respected (and not inherited from
        // from the parent).
        m_explicitTheme = true;
        if (m_theme == theme)
            return;
    
        m_theme = theme;
        propagateTheme();
        themeChange();
    
    }
    
    void MyStyle::inheritTheme(Theme theme)
    {
        if (m_explicitTheme || m_theme == theme)
            return;
    
        m_theme = theme;
        propagateTheme();
        themeChange();
    }
    
    void MyStyle::propagateTheme()
    {
        const auto styles = attachedChildren();
        for (QQuickAttachedPropertyPropagator *child : styles) {
            MyStyle *myStyle = qobject_cast<MyStyle *>(child);
            if (myStyle)
                myStyle->inheritTheme(m_theme);
        }
    }
    
    void MyStyle::resetTheme()
    {
        if (!m_explicitTheme)
            return;
    
        m_explicitTheme = false;
        MyStyle *myStyle = qobject_cast<MyStyle *>(attachedParent());
        inheritTheme(myStyle ? myStyle->theme() : globalTheme);
    }
  4. プロパティの継承を処理するために、attachedParentChange() を再実装します:
    void MyStyle::attachedParentChange(QQuickAttachedPropertyPropagator *newParent, QQuickAttachedPropertyPropagator *oldParent)
    {
        Q_UNUSED(oldParent);
        MyStyle *attachedParentStyle = qobject_cast<MyStyle *>(newParent);
        if (attachedParentStyle) {
            inheritTheme(attachedParentStyle->theme());
            // Do any other inheriting here...
        }
    }
  5. 「アタッチドプロパティの提供」で詳述されているように、静的な `qmlAttachedProperties ` 関数を実装し、`QML_ELEMENT ` および `QML_ATTACHED` を使用して、その型をアタッチド QML 型として宣言します:
    MyStyle *MyStyle::qmlAttachedProperties(QObject *object)
    {
        return new MyStyle(object);
    }

完全な実装例は、「Qt Quick コントロール - アタッチド・スタイル・プロパティの例」でご覧いただけます。

「 Qt Quick コントロールのスタイル設定」も参照してください 。

メンバ関数のドキュメント

[explicit] QQuickAttachedPropertyPropagator::QQuickAttachedPropertyPropagator(QObject *parent = nullptr)

指定されたparent を使用して、QQuickAttachedPropertyPropagatorを生成します。

parent は、このオブジェクトのattached parent を特定するために使用されます。

派生クラスは、コンストラクタ内でinitialize()を呼び出す必要があります。

[virtual noexcept] QQuickAttachedPropertyPropagator::~QQuickAttachedPropertyPropagator()

QQuickAttachedPropertyPropagator を破棄します。

QList<QQuickAttachedPropertyPropagator *> QQuickAttachedPropertyPropagator::attachedChildren() const

この関数は、このアタッチされたオブジェクトにアタッチされた子オブジェクトを返します。

アタッチされた子オブジェクトは、プロパティの値を伝播する際に使用されます:

void MyStyle::propagateTheme()
{
    const auto styles = attachedChildren();
    for (QQuickAttachedPropertyPropagator *child : styles) {
        MyStyle *myStyle = qobject_cast<MyStyle *>(child);
        if (myStyle)
            myStyle->inheritTheme(m_theme);
    }
}

QQuickAttachedPropertyPropagator *QQuickAttachedPropertyPropagator::attachedParent() const

この関数は、このアタッチされたオブジェクトのアタッチされた親を返します。

アタッチされた親は、プロパティの値を継承する際に使用されます:

void MyStyle::resetTheme()
{
    if (!m_explicitTheme)
        return;

    m_explicitTheme = false;
    MyStyle *myStyle = qobject_cast<MyStyle *>(attachedParent());
    inheritTheme(myStyle ? myStyle->theme() : globalTheme);
}

[virtual protected] void QQuickAttachedPropertyPropagator::attachedParentChange(QQuickAttachedPropertyPropagator *newParent, QQuickAttachedPropertyPropagator *oldParent)

この関数は、このQQuickAttachedPropertyPropagator の添付親がoldParent からnewParent に変更されるたびに呼び出されます。

サブクラスは、newParent から添付プロパティを継承するために、この関数を再実装する必要があります。

void MyStyle::attachedParentChange(QQuickAttachedPropertyPropagator *newParent, QQuickAttachedPropertyPropagator *oldParent)
{
    Q_UNUSED(oldParent);
    MyStyle *attachedParentStyle = qobject_cast<MyStyle *>(newParent);
    if (attachedParentStyle) {
        inheritTheme(attachedParentStyle->theme());
        // Do any other inheriting here...
    }
}

[protected] void QQuickAttachedPropertyPropagator::initialize()

このアタッチされたオブジェクトの親を検索・設定し、その後、その子オブジェクトに対しても同様の処理を行います。伝播が正しく機能するためには、アタッチされたオブジェクトの生成時にこの関数を呼び出す必要があります。

この関数を呼び出す前に、グローバル値やデフォルト値を読み取っておくと便利です。例えば、initialize() を呼び出す前に、Imagineスタイルは静的な「globalsInitialized」フラグをチェックし、QSettings からデフォルト値を読み取るべきかどうかを確認します。そのファイルの値は、明示的に設定されていないアタッチメントプロパティの値の基礎となります。

MyStyle::MyStyle(QObject *parent)
    : QQuickAttachedPropertyPropagator(parent)
    , m_theme(globalTheme)
{
    // A static function could be called here that reads globalTheme from a
    // settings file once at startup. That value would override the global
    // value. This is similar to what the Imagine and Material styles do, for
    // example.

    initialize();
}

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