QQuickAttachedPropertyPropagator Class
La classe `QQuickAttachedPropertyPropagator` permet de propager les propriétés associées. En savoir plus...
| 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 de : | 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'associer des propriétés et des gestionnaires de signaux à des objets. La section « Fournir des propriétés associées » explique plus en détail comment exposer vos propres types associés en C++.
QQuickAttachedPropertyPropagator fournit une API permettant de propager les propriétés associées d’un objet parent vers ses enfants, à l’instar de la propagation des objets « 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, envisagez plutôt d’utiliser un singleton C++ ou QML, car il est mieux adapté à ce cas d’utilisation et plus efficace dans la mesure où il ne nécessite qu’une seule QObject.
Pour implémenter une propriété attachée personnalisée :
- Dérivez une classe qui expose la propriété attachée à partir de `QQuickAttachedPropertyPropagator`.
Par exemple, pour implémenter une propriété attachée de type «
MyStyle.theme», déclarez la classe `MyStyle` :class MYSTYLE_EXPORT MyStyle : public QQuickAttachedPropertyPropagator - 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(); } - Définissez les fonctions set, inherit, propagate et reset pour la propriété attachée selon vos besoins. Par exemple, pour définir une propriété attachée de type «
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); } - Réimplémentez la méthode ` attachedParentChange()` pour gérer l'héritage de la propriété :
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... } } - Implémentez une fonction statique `
qmlAttachedProperties` et déclarez le type en tant que type QML attaché à l'aide de ` QML_ELEMENT ` et ` QML_ATTACHED`, comme détaillé dans la section « Fournir des propriétés attachées » :MyStyle *MyStyle::qmlAttachedProperties(QObject *object) { return new MyStyle(object); }
L'implémentation complète est disponible dans Qt Quick Controls - Exemple de propriétés de style associées.
Voir également «Stylisation d'Qt Quick Controls ».
Documentation des fonctions membres
[explicit] QQuickAttachedPropertyPropagator::QQuickAttachedPropertyPropagator(QObject *parent = nullptr)
Construit un objet `QQuickAttachedPropertyPropagator` avec l'parent` fournie.
L'parent sera utilisé pour déterminer la valeur de ` attached parent` de cet objet.
Les classes dérivées doivent appeler la méthode initialize() dans leur constructeur.
[virtual noexcept] QQuickAttachedPropertyPropagator::~QQuickAttachedPropertyPropagator()
Supprime l'QQuickAttachedPropertyPropagator.
QList<QQuickAttachedPropertyPropagator *> QQuickAttachedPropertyPropagator::attachedChildren() const
Cette fonction renvoie les enfants associés à cet objet.
Les enfants associés sont utilisés lors de la propagation des valeurs de propriété :
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 associé à cet objet associé.
Le parent associé est utilisé lors de l'héritage des valeurs de propriété :
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 associé à cette ` QQuickAttachedPropertyPropagator ` passe de ` oldParent ` à ` newParent`.
Les sous-classes doivent réimplémenter cette fonction pour hériter des propriétés associé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 associé à cet objet attaché, puis effectue la même opération pour ses enfants. Cette fonction doit être appelée lors de la création de l’objet attaché 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 déterminer s’il doit lire les valeurs par défaut à partir de ` QSettings`. Les valeurs de ce fichier servent de base à toutes les propriétés associé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.