QQuickAttachedPropertyPropagator Class | Qt Quick Controls 6.8.0
Description: The QQuickAttachedPropertyPropagator class provides a way to propagate attached properties.

- Qt 6.8
- Qt Quick Controls
- C++ Classes
- QQuickAttachedPropertyPropagator
- Qt 6.8.0 Reference Documentation

# QQuickAttachedPropertyPropagator Class

The QQuickAttachedPropertyPropagator class provides a way to propagate attached properties. More...

Header: | #include <QQuickAttachedPropertyPropagator>
CMake: | find_package(Qt6 REQUIRED COMPONENTS QuickControls2) target_link_libraries(mytarget PRIVATE Qt6::QuickControls2)
qmake: | QT += quickcontrols2
Since: | Qt 6.5
Inherits: | QObject

- List of all members, including inherited members

## Public Functions

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

## Protected Functions

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

## Detailed Description

In QML, it is possible to attach properties and signal handlers to objects. Providing Attached Properties goes into more detail about how to expose your own C++ attached types.

QQuickAttachedPropertyPropagator provides an API to propagate attached properties from a parent object to its children, similar to font and palette propagation. It supports propagation through items, popups, and windows.

If propagation of properties is not important, consider using a C++ or QML singleton instead, as it is better suited for that use case, and is more efficient in that it only requires one QObject.

To use QQuickAttachedPropertyPropagator:

- Derive from it
- Call initialize() in the constructor
- Define set/inherit/propagate/reset functions for each property as needed
- Reimplement attachedParentChange() to handle property inheritance

For an example that demonstrates this in depth, see Qt Quick Controls - Attached Style Properties Example.

See also Styling Qt Quick Controls.

## Member Function Documentation

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

Constructs a QQuickAttachedPropertyPropagator with the given parent.

The `parent` will be used to find this object's attached parent.

Derived classes should call initialize() in their constructor.

### `[virtual noexcept]` QQuickAttachedPropertyPropagator::~QQuickAttachedPropertyPropagator()

Destroys the QQuickAttachedPropertyPropagator.

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

This function returns the attached children of this attached object.

The attached children are used when propagating property values:

     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

This function returns the attached parent of this attached object.

The attached parent is used when inheriting property values:

     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)

This function is called whenever the attached parent of this QQuickAttachedPropertyPropagator changes from oldParent to newParent.

Subclasses should reimplement this function to inherit attached properties from `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()

Finds and sets the attached parent for this attached object, and then does the same for its children. This must be called upon construction of the attached object in order for propagation to work.

It can be useful to read global/default values before calling this function. For example, before calling `initialize()`, the Imagine style checks a static "globalsInitialized" flag to see if it should read default values from QSettings. The values from that file form the basis for any attached property values that have not been explicitly set.

     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();
     }