QQmlParserStatus Class

QQmlParserStatus 类提供 QML 解析器状态的更新。更多

Header: #include <QQmlParserStatus>
CMake: find_package(Qt6 REQUIRED COMPONENTS Qml)
target_link_libraries(mytarget PRIVATE Qt6::Qml)
qmake: QT += qml
继承于

QQuickItem

公共函数

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

成员函数文档

[pure virtual] void QQmlParserStatus::classBegin()

在类创建后,但在设置任何属性之前调用。

[pure virtual] void QQmlParserStatus::componentComplete()

在导致该实例化的根组件完成构建后调用。此时,所有静态值和绑定值都已分配给该类。

© 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.