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++アタッチド型を公開する方法について詳しく説明しています。

font palette QQuickAttachedPropertyPropagator は、親オブジェクトから子オブジェクトにアタッチドプロパティを伝搬するAPIを提供します。これは、itemspopupswindows を介したプロパゲーションをサポートしています。

QObjectプロパティの伝搬が重要でない場合は、C++QML のシングルトンを使用することを検討してください。

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

  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_ELEMENTQML_ATTACHED を用いて、QMLのアタッチド・プロパティとして宣言する:
    MyStyle *MyStyle::qmlAttachedProperties(QObject *object)
    {
        return new MyStyle(object);
    }

完全な実装はQt Quick Controls - Attached Style Properties Example にあります。

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

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