QQuickAttachedPropertyPropagator Class

The QQuickAttachedPropertyPropagator class provides a way to propagate attached properties. 更多...

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

公共函数

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

保护函数

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

详细描述

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 ,和 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
  • 调用 initialize () in the constructor
  • Define set/inherit/propagate/reset functions for each property as needed
  • 重实现 attachedParentChange () to handle property inheritance

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

另请参阅 风格化 Qt Quick Controls .

成员函数文档编制

[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] QQuickAttachedPropertyPropagator:: ~QQuickAttachedPropertyPropagator ()

销毁 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 改变从 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() 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();
}