In questa pagina

QQuickAttachedPropertyPropagator Class

La classe QQuickAttachedPropertyPropagator consente di propagare le proprietà associate. Per saperne di più...

Intestazione: #include <QQuickAttachedPropertyPropagator>
CMake: find_package(Qt6 REQUIRED COMPONENTS QuickControls2)
target_link_libraries(mytarget PRIVATE Qt6::QuickControls2)
qmake: QT += quickcontrols2
A partire da: Qt 6.5
Eredita da: QObject

Funzioni pubbliche

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

Funzioni protette

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

Descrizione dettagliata

In QML è possibile associare proprietà e gestori di segnali agli oggetti. La sezione " Fornitura di proprietà associate " fornisce ulteriori dettagli su come esporre i propri tipi associati in C++.

QQuickAttachedPropertyPropagator fornisce un'API per propagare le proprietà associate da un oggetto padre ai suoi figli, in modo simile alla propagazione di font e palette. Supporta la propagazione tramite items, popups e windows.

Se la propagazione delle proprietà non è importante, si consiglia di utilizzare invece un singleton C++ o QML, poiché è più adatto a tale caso d'uso ed è più efficiente in quanto richiede un solo QObject.

Per implementare una proprietà associata personalizzata:

  1. Derivare una classe che esponga la proprietà associata da `QQuickAttachedPropertyPropagator`.

    Ad esempio, per implementare una proprietà collegata di tipo ` MyStyle.theme `, dichiarare la classe ` MyStyle `:

    class MYSTYLE_EXPORT MyStyle : public QQuickAttachedPropertyPropagator
  2. Chiamare ` initialize()` nel costruttore della propria 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. Definire le funzioni set/inherit/propagate/reset per la proprietà associata secondo necessità. Ad esempio, per definire una proprietà associata di tipo 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. Reimplementa attachedParentChange() per gestire l'ereditarietà delle proprietà:
    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. Implementare una funzione statica ` qmlAttachedProperties ` e dichiarare il tipo come tipo QML associato utilizzando ` QML_ELEMENT ` e ` QML_ATTACHED`, come descritto in dettaglio in "Fornitura di proprietà associate":
    MyStyle *MyStyle::qmlAttachedProperties(QObject *object)
    {
        return new MyStyle(object);
    }

L'implementazione completa è disponibile in " Qt Quick Controls - Attached Style Properties Example".

Vedi anche "Styling Qt Quick Controls".

Documentazione delle funzioni membro

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

Crea un oggetto `QQuickAttachedPropertyPropagator` con l'parent` specificato.

L'parent verrà utilizzato per individuare l'attached parent di questo oggetto.

Le classi derivate dovrebbero chiamare ` initialize()` nel proprio costruttore.

[virtual noexcept] QQuickAttachedPropertyPropagator::~QQuickAttachedPropertyPropagator()

Distrugge l'QQuickAttachedPropertyPropagator.

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

Questa funzione restituisce i figli associati a questo oggetto associato.

I figli associati vengono utilizzati durante la propagazione dei valori delle proprietà:

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

Questa funzione restituisce il genitore associato a questo oggetto associato.

Il genitore associato viene utilizzato quando si ereditano i valori delle proprietà:

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)

Questa funzione viene chiamata ogni volta che il genitore associato a questo oggetto ` QQuickAttachedPropertyPropagator ` passa da ` oldParent ` a ` newParent`.

Le sottoclassi dovrebbero reimplementare questa funzione per ereditare le proprietà associate da 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()

Trova e imposta il genitore associato a questo oggetto collegato, quindi esegue la stessa operazione per i suoi figli. Questa funzione deve essere chiamata al momento della creazione dell’oggetto collegato affinché la propagazione funzioni correttamente.

Può essere utile leggere i valori globali/predefiniti prima di chiamare questa funzione. Ad esempio, prima di chiamare ` initialize()`, lo stile Imagine controlla un flag statico denominato "globalsInitialized" per verificare se deve leggere i valori predefiniti da ` QSettings`. I valori contenuti in quel file costituiscono la base per tutti i valori delle proprietà associate che non sono stati impostati esplicitamente.

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.