QQuickAttachedPropertyPropagator Class
A classe QQuickAttachedPropertyPropagator oferece uma maneira de propagar propriedades anexadas. Mais...
| Cabeçalho: | #include <QQuickAttachedPropertyPropagator> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS QuickControls2) target_link_libraries(mytarget PRIVATE Qt6::QuickControls2) |
| qmake: | QT += quickcontrols2 |
| Desde: | Qt 6.5 |
| Herdado de: | QObject |
Funções públicas
| QQuickAttachedPropertyPropagator(QObject *parent = nullptr) | |
| virtual | ~QQuickAttachedPropertyPropagator() |
| QList<QQuickAttachedPropertyPropagator *> | attachedChildren() const |
| QQuickAttachedPropertyPropagator * | attachedParent() const |
Funções protegidas
| virtual void | attachedParentChange(QQuickAttachedPropertyPropagator *newParent, QQuickAttachedPropertyPropagator *oldParent) |
| void | initialize() |
Descrição detalhada
No QML, é possível anexar propriedades e manipuladores de sinal a objetos. A seção “Fornecendo propriedades anexadas” aborda mais detalhadamente como expor seus próprios tipos anexados em C++.
O `QQuickAttachedPropertyPropagator` fornece uma API para propagar propriedades associadas de um objeto pai para seus filhos, de forma semelhante à propagação de ` font ` e ` palette `. Ele suporta a propagação por meio de ` items`, ` popups` e ` windows`.
Se a propagação de propriedades não for importante, considere usar um singleton em C++ ou QML, pois ele é mais adequado para esse caso de uso e é mais eficiente, já que requer apenas um ` QObject`.
Para implementar uma propriedade anexada personalizada:
- Derive uma classe que exponha a propriedade anexada a partir de `QQuickAttachedPropertyPropagator`.
Por exemplo, para implementar uma propriedade anexada
MyStyle.theme, declare a classeMyStyle:class MYSTYLE_EXPORT MyStyle : public QQuickAttachedPropertyPropagator - Chame initialize() no construtor da sua 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(); } - Defina as funções set/inherit/propagate/reset para a propriedade anexada, conforme necessário. Por exemplo, para definir uma propriedade anexada
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); } - Reimplemente ` attachedParentChange()` para lidar com a herança de propriedades:
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... } } - Implemente uma função estática `
qmlAttachedProperties` e declare o tipo como um tipo QML anexado com ` QML_ELEMENT ` e ` QML_ATTACHED`, conforme detalhado em “Fornecendo propriedades anexadas”:MyStyle *MyStyle::qmlAttachedProperties(QObject *object) { return new MyStyle(object); }
A implementação completa está disponível em Controles `Qt Quick ` — Exemplo de propriedades de estilo anexadas.
Veja também Estilizando controles do ` Qt Quick `.
Documentação da função de membro
[explicit] QQuickAttachedPropertyPropagator::QQuickAttachedPropertyPropagator(QObject *parent = nullptr)
Cria um QQuickAttachedPropertyPropagator com o parent fornecido.
O ` parent ` será usado para localizar o ` attached parent` deste objeto.
As classes derivadas devem chamar ` initialize()` em seu construtor.
[virtual noexcept] QQuickAttachedPropertyPropagator::~QQuickAttachedPropertyPropagator()
Destrói o QQuickAttachedPropertyPropagator.
QList<QQuickAttachedPropertyPropagator *> QQuickAttachedPropertyPropagator::attachedChildren() const
Esta função retorna os filhos associados a este objeto associado.
Os filhos associados são utilizados na propagação de valores de propriedades:
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 função retorna o pai associado a este objeto associado.
O pai associado é usado ao herdar valores de propriedades:
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)
Essa função é chamada sempre que o pai associado a este ` QQuickAttachedPropertyPropagator ` muda de ` oldParent ` para ` newParent`.
As subclasses devem reimplementar essa função para herdar as propriedades associadas 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()
Localiza e define o pai associado para este objeto anexado e, em seguida, faz o mesmo com seus filhos. Esta função deve ser chamada durante a construção do objeto anexado para que a propagação funcione.
Pode ser útil ler os valores globais/padrão antes de chamar essa função. Por exemplo, antes de chamar ` initialize()`, o estilo Imagine verifica um sinalizador estático “globalsInitialized” para determinar se deve ler os valores padrão de ` QSettings`. Os valores desse arquivo servem de base para quaisquer valores de propriedades associadas que não tenham sido definidos explicitamente.
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.