Sur cette page

QQuickAttachedPropertyPropagator Class

La classe QQuickAttachedPropertyPropagator permet de propager les propriétés attachées. Plus d'informations...

En-tête : #include <QQuickAttachedPropertyPropagator>
CMake : find_package(Qt6 REQUIRED COMPONENTS QuickControls2)
target_link_libraries(mytarget PRIVATE Qt6::QuickControls2)
qmake : QT += quickcontrols2
Depuis : Qt 6.5
Hérite : QObject

Fonctions publiques

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

Fonctions protégées

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

Description détaillée

En QML, il est possible d'attacher des propriétés et des gestionnaires de signaux aux objets. Fournir des propriétés attachées explique en détail comment exposer vos propres types attachés en C++.

QQuickAttachedPropertyPropagator fournit une API pour propager les propriétés attachées d'un objet parent à ses enfants, de manière similaire à font et palette. Il prend en charge la propagation via items, popups, et windows.

Si la propagation des propriétés n'est pas importante, il est préférable d'utiliser un singleton C++ ou QML, qui est mieux adapté à ce cas d'utilisation et plus efficace car il ne nécessite qu'une seule adresse QObject.

Pour mettre en œuvre une propriété attachée personnalisée :

  1. Dérivez une classe qui expose la propriété attachée de QQuickAttachedPropertyPropagator.

    Par exemple, pour mettre en œuvre une propriété attachée MyStyle.theme, déclarez la classe MyStyle:

    class MYSTYLE_EXPORT MyStyle : public QQuickAttachedPropertyPropagator
  2. Appelez initialize() dans le constructeur de votre classe :
    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. Définissez des fonctions set/inherit/propagate/reset pour la propriété attachée si nécessaire. Par exemple, pour définir une propriété attachée 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. Réimplémentez attachedParentChange() pour gérer l'héritage des propriétés :
    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. Implémenter une fonction statique qmlAttachedProperties et déclarer le type comme un type QML attaché avec QML_ELEMENT et QML_ATTACHED, comme détaillé dans Fournir des propriétés attachées:
    MyStyle *MyStyle::qmlAttachedProperties(QObject *object)
    {
        return new MyStyle(object);
    }

L'implémentation complète est disponible sur Qt Quick Controls - Attached Style Properties Example.

Voir également Styling Qt Quick Controls.

Documentation sur les fonctions membres

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

Construit un QQuickAttachedPropertyPropagator avec l'adresse parent.

L'adresse parent sera utilisée pour trouver l'adresse attached parent de cet objet.

Les classes dérivées doivent appeler initialize() dans leur constructeur.

[virtual noexcept] QQuickAttachedPropertyPropagator::~QQuickAttachedPropertyPropagator()

Détruit le site QQuickAttachedPropertyPropagator.

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

Cette fonction renvoie les enfants attachés à cet objet attaché.

Les enfants attachés sont utilisés lors de la propagation des valeurs des propriétés :

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

Cette fonction renvoie le parent attaché de cet objet attaché.

Le parent attaché est utilisé lors de l'héritage des valeurs des propriétés :

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)

Cette fonction est appelée chaque fois que le parent attaché de cette QQuickAttachedPropertyPropagator passe de oldParent à newParent.

Les sous-classes doivent réimplémenter cette fonction pour hériter des propriétés attachées 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()

Recherche et définit le parent rattaché à cet objet rattaché, puis fait de même pour ses enfants. Cette fonction doit être appelée lors de la construction de l'objet rattaché pour que la propagation fonctionne.

Il peut être utile de lire les valeurs globales/par défaut avant d'appeler cette fonction. Par exemple, avant d'appeler initialize(), le style Imagine vérifie un indicateur statique "globalsInitialized" pour voir s'il doit lire les valeurs par défaut de QSettings. Les valeurs de ce fichier constituent la base de toutes les valeurs de propriétés attachées qui n'ont pas été explicitement définies.

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.