QQuickAttachedPropertyPropagator Class
QQuickAttachedPropertyPropagator 类提供了一种传播附加属性的方法。更多
Header: | #include <QQuickAttachedPropertyPropagator> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS QuickControls2) target_link_libraries(mytarget PRIVATE Qt6::QuickControls2) |
qmake: | QT += quickcontrols2 |
自 | 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() |
详细说明
在 QML 中,可以为对象附加属性和信号处理器。提供附加属性》(Providing Attached Properties)详细介绍了如何公开自己的 C++ 附加类型。
QQuickAttachedPropertyPropagator 提供了从父对象向子对象传播附加属性的 API,类似于font 和palette 传播。它支持通过items 、popups 和windows 传播。
如果属性传播并不重要,可以考虑使用C++ 或QML单例,因为它更适合这种用例,而且只需要一个QObject ,效率更高。
实现自定义附加属性
- 从 QQuickAttachedPropertyPropagator 派生一个公开附加属性的类。
例如,要实现
MyStyle.theme
附加属性,请声明MyStyle
类:class MYSTYLE_EXPORT MyStyle : public QQuickAttachedPropertyPropagator
- 在类的构造函数中调用initialize() :
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(); }
- 根据需要为附加属性定义 set/inherit/propagate/reset 函数。例如,定义一个
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); }
- 重新实现attachedParentChange() 以处理属性继承:
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... } }
- 实现静态
qmlAttachedProperties
函数,并用QML_ELEMENT 和QML_ATTACHED 声明该类型为附加 QML 类型,详见Providing Attached Properties(提供附加属性):MyStyle *MyStyle::qmlAttachedProperties(QObject *object) { return new MyStyle(object); }
完整的实现可参见Qt Quick Controls - 附加样式属性示例。
另请参阅 样式Qt Quick Controls 。
成员函数文档
[explicit]
QQuickAttachedPropertyPropagator::QQuickAttachedPropertyPropagator(QObject *parent = nullptr)
用给定的parent 构建一个 QQuickAttachedPropertyPropagator。
parent
将用于查找此对象的attached parent 。
派生类应在其构造函数中调用initialize() 。
[virtual noexcept]
QQuickAttachedPropertyPropagator::~QQuickAttachedPropertyPropagator()
摧毁QQuickAttachedPropertyPropagator.
QList<QQuickAttachedPropertyPropagator *> QQuickAttachedPropertyPropagator::attachedChildren() const
此函数返回此附加对象的附加子对象。
附加子对象用于传播属性值:
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
此函数返回此附加对象的父附加对象。
附加父对象用于继承属性值:
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)
每当QQuickAttachedPropertyPropagator 的附加父类从oldParent 变为newParent 时,都会调用此函数。
子类应重新实现此函数,以继承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()
查找并设置此附加对象的父对象,然后对其子对象执行同样的操作。必须在构建附加对象时调用此函数,才能使传播生效。
在调用此函数之前,读取全局/默认值可能会很有用。例如,在调用initialize()
之前,Imagine风格会检查静态 "globalsInitialized "标志,以确定是否应从QSettings 中读取默认值。该文件中的值是任何未明确设置的附加属性值的基础。
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(); }
© 2025 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.