QQmlParserStatus Class
QQmlParserStatus 类提供有关 QML 解析器状态的更新信息。更多内容...
| 标题: | #include <QQmlParserStatus> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Qml) target_link_libraries(mytarget PRIVATE Qt6::Qml) |
| qmake: | QT += qml |
| 由以下接口继承: |
公共函数
| virtual void | classBegin() = 0 |
| virtual void | componentComplete() = 0 |
详细说明
QQmlParserStatus 为由QQmlEngine 实例化的类提供了一种机制,使其能够在创建过程中的关键节点接收通知。
该类通常用于优化目的,因为它允许您将耗时的操作推迟到对象的所有属性都已设置之后再执行。 例如,QML 的Text 元素使用解析器状态将文本布局推迟到所有属性都已设置之后(我们不希望在text 被赋值时进行布局,然后在font 被赋值时重新布局,再在width 被赋值时再次重新布局,以此类推)。
请注意,QQmlParserStatus 的方法仅在类由QQmlEngine 实例化时才会被调用。如果您直接从 C++ 创建同一类,这些方法将不会自动被调用。 为避免此问题,建议您从classBegin 开始推迟操作,而不是从类的初始创建开始。这样既能防止在QML中的初始绑定赋值期间发生多次重新评估,又不会推迟从C++调用的操作。
要使用 QQmlParserStatus,您必须同时继承自QObject 派生的类和 QQmlParserStatus,并使用Q_INTERFACES() 宏。
class MyObject : public QObject, public QQmlParserStatus
{
Q_OBJECT
Q_INTERFACES(QQmlParserStatus)
public:
MyObject(QObject *parent = nullptr);
// ...
void classBegin() override;
void componentComplete() override;
};© 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.