QQmlSA::ElementPass Class
class QQmlSA::ElementPass所有针对元素的静态分析阶段的基类。更多...
| 头文件: | #include <qqmlsa.h> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS QmlCompiler) target_link_libraries(mytarget PRIVATE Qt6::QmlCompiler) |
| 继承自: | QQmlSA::GenericPass |
| 状态: | 技术预览 |
该类处于技术预览阶段,内容可能会有变动。
公共函数
| ElementPass(QQmlSA::PassManager *manager) | |
| virtual void | run(const QQmlSA::Element &element) = 0 |
| virtual bool | shouldRun(const QQmlSA::Element &element) |
详细说明
ElementPass 是两种分析阶段中较简单的一种。它将处理文件中的每个元素。可使用shouldRun() 方法过滤掉无关元素,而run() 方法则负责执行初步处理工作。
适合 ElementPass 的常见任务包括
- 检查元素的属性是否以无意义的方式组合在一起
- 验证属性值(例如,确保某个属性仅接受特定的枚举值)
- 检查依赖于元素父节点的行为(例如,当父元素是
Layout时,不使用Item::width )。
如下面的代码片段所示,建议在 ElementPass 的构造函数中进行必要的类型解析并将其缓存到局部成员中,同时通过 `shouldRun()` 实现一些过滤,以保持静态分析的性能。
using namespace QQmlSA;
class MyElementPass : public ElementPass
{
Element myType;
public:
MyElementPass(QQmlSA::PassManager *manager)
: myType(resolveType("MyModule", "MyType")) {}
bool shouldRun(const Element &element) override
{
return element.inherits(myType);
}
void run(const Element &element) override
{
// actual pass logic
}
}ElementPass 对元素属性的使用情况了解有限。若需要此类信息,请考虑改用PropertyPass 。
注意:ElementPass 仅会考虑可实例化的类型。因此,它不适用于分析附加类型和单例。这些需要通过PropertyPass 来处理。
成员函数文档
ElementPass::ElementPass(QQmlSA::PassManager *manager)
创建一个 ElementPass 对象,并使用 `manager ` 来引用传递管理器。
[pure virtual] void ElementPass::run(const QQmlSA::Element &element)
当shouldRun() 返回true 时执行。在element 上执行该处理阶段的实际计算。此方法旨在被重写。无需调用基类方法。
[virtual] bool ElementPass::shouldRun(const QQmlSA::Element &element)
控制是否应在给定的element 上执行run() 函数。子类可以重写此方法,通过过滤掉不相关的元素来提高分析性能。
默认实现会无条件地返回true 。
© 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.