本页内容

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 提供了一组 API,用于将附加属性从父对象传播到其子对象,这与font 和palette 的传播机制类似。它支持通过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. 根据需要为该附加属性定义 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 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.