分析 QML 代码
借助“QML Profiler ”,您可以找出Qt Quick 应用程序中常见的性能问题(例如运行缓慢、无响应以及用户界面卡顿)的根源。
您可以通过 VS Code 的 Qt 扩展启动QML Profiler ,对正在运行的 QML 应用程序进行性能分析,并分析性能分析数据。
安装 QML Profiler
要对 QML 应用程序进行性能分析,您需要安装 QML Profiler。请访问Command Palette 并选择“Qt: Install the most recent QML trace viewer ”。
如果您尚未安装QML Profiler ,当您首次开始对应用程序进行性能分析时,VS Code 的 Qt 扩展会提示您进行安装。
若要使用特定的QML Profiler 可执行文件,请在QML Profiler 的设置中设置其路径。
收集性能分析数据
要收集当前打开的应用程序的性能分析数据:
- 转到Command Palette ,并在工具栏上选择“Qt: Profile QML application ”或“
”(Profile QML application )。 - 使用该应用程序收集性能分析数据。
- 关闭应用程序,或在工具栏上选择“
”(Stop QML profiler )。
使用“QML Profiler ”收集数据并分析结果。

有关详细信息,请参阅“Qt Creator ”文档中的“对 QML 应用程序进行性能分析”。
您可以在“QML Profiler ”设置中设置在启动“QML Profiler ”时传递给应用程序的参数。
将QML Profiler 附加到正在运行的应用程序
要将QML Profiler 附加到正在运行的应用程序以收集性能分析数据:
- 在Terminal 中,使用以下参数启动应用程序:
<your_executable_path> -qmljsdebugger=host:<IP_address>,port:<port_number>,block,services:QmlProfiler,CanvasFrameRate
其中IP_address是应用程序运行主机的 IP 地址,port_number是调试端口,block 参数可阻止应用程序运行,直到调试客户端连接到服务器为止。这将使性能分析从应用程序启动时就开始进行。
- 当应用程序正在运行时,访问Command Palette ,并选择“Qt: Attach to running QML application for profiling ”。
- 输入主机的 IP 地址,然后选择“Enter”。
- 输入端口号,然后选择“Enter”。
另请参阅 Qt 设置。
© 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.