QQuickAttachedPropertyPropagator Class

QQuickAttachedPropertyPropagator 클래스는 첨부된 프로퍼티를 전파하는 방법을 제공합니다. 더 보기...

Header: #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는 fontpalette 전파 방식과 유사하게 부모 객체에서 자식 객체로 첨부 프로퍼티를 전파하는 API를 제공합니다. items , popups, windows 를 통한 전파를 지원합니다.

프로퍼티의 전파가 중요하지 않은 경우에는 C++ 또는 QML 싱글톤을 사용하는 것이 해당 사용 사례에 더 적합하고 QObject 하나만 필요하다는 점에서 더 효율적이므로 대신 사용하는 것이 좋습니다.

사용자 지정 첨부 프로퍼티를 구현하려면 다음과 같이 하세요:

  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. 필요에 따라 첨부된 프로퍼티에 대한 설정/상속/전파/리셋 함수를 정의합니다. 예를 들어 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 - 첨부된 스타일 속성 예제에서 확인할 수 있습니다.

스타일 지정 Qt Quick Controls참조하세요 .

멤버 함수 문서

[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()

이 첨부된 객체에 대한 첨부된 부모를 찾아 설정한 다음, 그 자식에 대해서도 동일한 작업을 수행합니다. 전파가 작동하려면 첨부된 객체를 생성할 때 이 함수를 호출해야 합니다.

이 함수를 호출하기 전에 전역/기본값을 읽어두면 유용할 수 있습니다. 예를 들어 Imagine 스타일은 initialize() 을 호출하기 전에 정적 "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.