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에서는 객체에 속성과 신호 핸들러를 연결할 수 있습니다. ‘Attached Properties 제공’ 섹션에서는 사용자 정의 C++ 연결형(attached type)을 노출하는 방법에 대해 더 자세히 설명합니다.
QQuickAttachedPropertyPropagator는 font 및 palette 전파와 유사하게, 부모 객체에서 자식 객체로 부착된 속성을 전파하는 API를 제공합니다. 이 클래스는 items, popups 및 windows 를 통한 전파를 지원합니다.
속성 전파가 중요하지 않다면, 해당 사용 사례에 더 적합하고 QObject 가 하나만 필요하다는 점에서 더 효율적인 C++ 또는 QML 싱글톤을 대신 사용하는 것을 고려해 보십시오.
사용자 정의 부착 속성을 구현하려면:
- 부착 속성을 노출하는 클래스를 QQuickAttachedPropertyPropagator에서 파생시킵니다.
예를 들어,
MyStyle.theme속성을 구현하려면MyStyle클래스를 다음과 같이 선언합니다:class MYSTYLE_EXPORT MyStyle : public QQuickAttachedPropertyPropagator - 클래스의 생성자에서 ` 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(); } - 필요에 따라 부착 속성에 대한 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); } - 속성 상속을 처리하려면 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... } } - '부착 속성 제공(Providing Attached Properties)' 섹션에 자세히 설명된 대로, 정적
qmlAttachedProperties함수를 구현하고 QML_ELEMENT 및 QML_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()
이 부착된 객체의 부착된 부모를 찾아 설정하고, 그 자식들에 대해서도 동일한 작업을 수행합니다. 전파 기능이 정상적으로 작동하려면 이 함수는 부착된 객체가 생성될 때 호출되어야 합니다.
이 함수를 호출하기 전에 전역/기본값을 읽어두면 유용할 수 있습니다. 예를 들어, ` 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.