En esta página

QQuickAttachedPropertyPropagator Class

La clase QQuickAttachedPropertyPropagator proporciona una forma de propagar propiedades adjuntas. Más...

Cabecera: #include <QQuickAttachedPropertyPropagator>
CMake: find_package(Qt6 REQUIRED COMPONENTS QuickControls2)
target_link_libraries(mytarget PRIVATE Qt6::QuickControls2)
qmake: QT += quickcontrols2
Desde: Qt 6.5
Hereda: QObject

Funciones Públicas

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

Funciones Protegidas

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

Descripción detallada

En QML, es posible adjuntar propiedades y manejadores de señales a los objetos. Providing Attached Properties entra en más detalle sobre cómo exponer sus propios tipos adjuntos C++.

QQuickAttachedPropertyPropagator proporciona una API para propagar propiedades adjuntas desde un objeto padre a sus hijos, similar a la propagación de font y palette. Soporta la propagación a través de items, popups, y windows.

Si la propagación de propiedades no es importante, considere utilizar un singleton C++ o QML en su lugar, ya que es más adecuado para ese caso de uso, y es más eficiente en el sentido de que sólo requiere un QObject.

Para implementar una propiedad adjunta personalizada

  1. Derive una clase que exponga la propiedad adjunta de QQuickAttachedPropertyPropagator.

    Por ejemplo, para implementar una propiedad adjunta MyStyle.theme, declare la clase MyStyle:

    class MYSTYLE_EXPORT MyStyle : public QQuickAttachedPropertyPropagator
  2. Llame a initialize() en el constructor de su clase:
    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. Defina funciones set/inherit/propagate/reset para la propiedad adjunta según sea necesario. Por ejemplo, para definir una propiedad adjunta 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. Reimplemente attachedParentChange() para manejar la herencia de propiedades:
    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. Implemente una función estática qmlAttachedProperties y declare el tipo como un tipo QML adjunto con QML_ELEMENT y QML_ATTACHED, como se detalla en Proporcionar propiedades adjuntas:
    MyStyle *MyStyle::qmlAttachedProperties(QObject *object)
    {
        return new MyStyle(object);
    }

La implementación completa está disponible en Qt Quick Controls - Attached Style Properties Example.

Véase también Estilización Qt Quick Controls.

Documentación de funciones miembro

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

Construye un QQuickAttachedPropertyPropagator con la dirección parent.

El parent se utilizará para encontrar el attached parent de este objeto.

Las clases derivadas deben llamar a initialize() en su constructor.

[virtual noexcept] QQuickAttachedPropertyPropagator::~QQuickAttachedPropertyPropagator()

Destruye el QQuickAttachedPropertyPropagator.

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

Esta función devuelve los hijos adjuntos de este objeto adjunto.

Los hijos adjuntos se utilizan al propagar valores de propiedades:

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

Esta función devuelve el padre adjunto de este objeto adjunto.

El padre adjunto se utiliza cuando se heredan valores de propiedad:

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)

Esta función es llamada cada vez que el padre adjunto de este QQuickAttachedPropertyPropagator cambia de oldParent a newParent.

Las subclases deben reimplementar esta función para heredar las propiedades adjuntas de 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()

Encuentra y establece el padre adjunto para este objeto adjunto, y luego hace lo mismo para sus hijos. Esta función debe invocarse al construir el objeto adjunto para que funcione la propagación.

Puede ser útil leer los valores globales/por defecto antes de llamar a esta función. Por ejemplo, antes de llamar a initialize(), el estilo Imagine comprueba una bandera estática "globalsInitialized" para ver si debe leer los valores por defecto de QSettings. Los valores de ese archivo forman la base para cualquier valor de propiedad adjunta que no se haya establecido explícitamente.

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.