QQuickAttachedPropertyPropagator Class
La clase `QQuickAttachedPropertyPropagator` ofrece una forma de propagar las propiedades asociadas. Más información...
| Encabezado: | #include <QQuickAttachedPropertyPropagator> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS QuickControls2) target_link_libraries(mytarget PRIVATE Qt6::QuickControls2) |
| qmake: | QT += quickcontrols2 |
| Desde: | Qt 6.5 |
| Heredan de: | 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 asociar propiedades y controladores de señales a los objetos. La sección «Proporcionar propiedades asociadas » ofrece más detalles sobre cómo exponer tus propios tipos asociados en C++.
QQuickAttachedPropertyPropagator proporciona una API para propagar propiedades adjuntas desde un objeto padre a sus hijos, de forma similar a la propagación de « font » y « palette ». Admite la propagación a través de « items », « popups » y « windows ».
Si la propagación de propiedades no es importante, plantéate utilizar en su lugar un singleton de C++ o QML, ya que se adapta mejor a ese caso de uso y es más eficiente, ya que solo requiere un QObject.
Para implementar una propiedad adjunta personalizada:
- Deriva una clase que exponga la propiedad adjunta a partir de `QQuickAttachedPropertyPropagator`.
Por ejemplo, para implementar una propiedad adjunta de tipo `
MyStyle.theme`, declara la clase `MyStyle`:class MYSTYLE_EXPORT MyStyle : public QQuickAttachedPropertyPropagator - Llama a ` initialize()` en el constructor de tu 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(); } - Define las funciones set, inherit, propagate y 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); } - Reimplementa attachedParentChange() para gestionar 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... } } - Implemente una función estática `
qmlAttachedProperties` y declare el tipo como un tipo QML adjunto mediante ` QML_ELEMENT ` y ` QML_ATTACHED`, tal y 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 - Ejemplo de propiedades de estilo adjuntas».
Véase también «Estilización decontroles de Qt Quick ».
Documentación de las funciones de miembro
[explicit] QQuickAttachedPropertyPropagator::QQuickAttachedPropertyPropagator(QObject *parent = nullptr)
Crea un objeto `QQuickAttachedPropertyPropagator` con el ` parent` indicado.
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 elementos secundarios asociados a este objeto asociado.
Los elementos secundarios asociados se utilizan al propagar los valores de las 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 objeto padre asociado a este objeto asociado.
El padre asociado se utiliza al heredar valores de propiedades:
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 se invoca cada vez que el padre asociado a este ` QQuickAttachedPropertyPropagator ` cambia de ` oldParent ` a ` newParent`.
Las subclases deben reimplementar esta función para heredar las propiedades asociadas 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()
Busca y establece el objeto padre asociado a este objeto adjunto y, a continuación, hace lo mismo con sus hijos. Esta función debe llamarse durante la construcción del objeto adjunto para que la propagación funcione correctamente.
Puede resultar útil leer los valores globales o predeterminados antes de llamar a esta función. Por ejemplo, antes de llamar a ` initialize()`, el estilo Imagine comprueba un indicador estático «globalsInitialized» para ver si debe leer los valores predeterminados de ` QSettings`. Los valores de ese archivo constituyen la base para cualquier valor de propiedad asociada 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.