调试Qt Quick 应用程序
您可以使用 QML 调试器和所用操作系统的调试器,对同时包含 QML 和 C/C++ 代码的Qt Quick 应用程序进行调试。通常,在 Linux 和 macOS 上使用 GDB,在 Windows 上使用 Visual Studio Windows 调试器。
使用 QML 调试器时,您可以:
- 显示变量(包括“此变量”
- 操作变量的值
- 添加监视表达式
- 添加条件断点
- 添加表达式求值
注意:调试 需要在 TCP 端口上打开一个套接字,这会带来安全风险。互联网上的任何人都可以连接到您正在调试的应用程序,并执行任何 JavaScript 函数。因此,您必须确保该端口已通过防火墙得到妥善保护。
启动和调试应用程序
若要仅调试应用程序中的 QML 代码,您可以先启动应用程序,然后将 QML 调试器附加到该应用程序上。
要启动并调试Qt Quick 应用程序:
- 为项目创建一个
launch.json文件。有关详细信息,请参阅《调试 Qt 应用程序》。 - 选择“QML debugger ”。
- 选择“Qt: QML: Launch ”调试配置。
该启动调试配置会在launch.json 文件中添加以下内容:
"configurations": [
{
"name": "Qt: QML: Launch",
"type": "qml",
"request": "launch",
"program": "${command:cmake.launchTargetPath}"
},
{
"name": "Qt: QML: Attach by port",
"type": "qml",
"request": "attach",
"host": "localhost",
"port": "^\"Custom port or \\${command:qt-qml.debugPort} for compound usage\""
}
]附加到正在运行的应用程序
若仅需调试应用程序中的 Qml 代码,使用固定端口号将调试器附加到正在运行的应用程序,比让 Qt Extension for VS Code 先启动应用程序再附加 Qml 调试器要快。
若仅需调试 QML 代码:
- 为项目创建一个launch.json文件。有关详细信息,请参阅《调试 Qt 应用程序》。
- 选择“Add Configuration ”,然后选择“Qt: QML: Attach by port ”调试配置。
- 在“Terminal ”中,使用以下参数启动应用程序:
<your_executable_path> -qmljsdebugger=host:<IP_address>,port:<port_number>,block,services:DebugMessages,QmlDebugger,V8Debugger,QmlInspector
其中,IP_address是应用程序运行的主机 IP 地址,port_number是调试端口,block 参数可阻止应用程序运行,直到调试客户端连接到服务器为止。这将使应用程序从启动时即可进行调试。
- 将端口号设置为launch.json文件中
port选项的值:"configurations": [ { "name": "Qt: QML: Attach by port", "type": "qml", "request": "attach", "host": "localhost", "port": "<port_number>" },
设置构建文件夹
如果构建文件夹位于工作区文件夹之外,Qt Extension for VS Code 需要知道其位置,以便查找与虚拟和物理 QML 文件对应的 QRC 文件。要设置构建文件夹,请将其添加到“Qt: QML: Launch ”或“Qt: QML: Attach by port ”调试配置中,作为“buildDirs ”的值。
例如:
"configurations": [
{
"name": "Qt: QML: Launch",
"type": "qml",
"request": "launch",
"program": "${command:cmake.launchTargetPath}",
"buildDirs": [
"<my_build_folder_outside_workspace>",
"<my_build_folder_outside_workspace_2>",
]
}
]覆盖默认参数
若要覆盖默认调试器参数,请将参数作为debuggerArgs 的值添加到Qt: QML: Launch 或Qt: QML: Attach by port 调试配置中。
例如:
"configurations": [
{
"name": "Qt: QML: Launch",
"type": "qml",
"request": "launch",
"program": "${command:cmake.launchTargetPath}",
"debuggerArgs": "-qmljsdebugger=<arguments>",
}
]向可执行文件传递参数
要向可执行文件传递参数,请将这些参数作为args 的值添加到Qt: QML: Launch 或Qt: QML: Attach by port 调试配置中。
例如:
"configurations": [
{
"name": "Qt: QML: Launch",
"type": "qml",
"request": "launch",
"program": "${command:cmake.launchTargetPath}",
"args": [
"<argument>",
"<argument> <argument>",
]
}
]调试混合的 C/C++ 和 QML 代码
您可以创建一个调试配置,该配置首先启动应用程序,然后使用“Qt: Acquire Port ”预启动任务获取的端口号,将 QML 和 C/C++ 调试器同时附加到该应用程序上。
要调试包含 C/C++ 和 QML 代码的Qt Quick 应用程序:
- 打开一个launch.json文件。有关详细信息,请参阅《调试 Qt 应用程序》。
- 选择“Add Configuration ”,然后选择与您的调试器匹配的“Qt: Debug with cppdbg and QML debugger ”或“Qt: Debug with cppvsdbg and QML debugger (Windows) ”调试配置。
- 在launch.json文件中添加用于 C++/QML 调试的复合启动任务和预启动任务:)
"compounds": [ { "name": "C++/QML", "configurations": ["Qt: Debug with cppdbg and QML debugger", "Qt: QML: Attach by port"], "preLaunchTask": "Qt: Acquire Port", } ]"compounds": [ { "name": "C++/QML", "configurations": ["Qt: Debug with cppvsdbg and QML debugger (Windows)", "Qt: QML: Attach by port"], "preLaunchTask": "Qt: Acquire Port", } ]
另请参阅 《调试 Qt 应用程序》、《调试Qt for Python 应用程序》和《VS Code:调试》。
© 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.