调试 Qt 应用程序
您可以在应用程序终止运行之前中断其运行,或查明应用程序无法正常运行的原因。设置断点可暂停应用程序,以便检查和修改变量、设置新断点或删除旧断点,然后继续运行应用程序。
当应用程序暂停时,您可以在调试器中检查特定数据。数据的可用性取决于编译应用程序时的编译器设置以及应用程序暂停的确切位置。使用 VS Code 的标准功能逐步执行代码并检查变量的变化:步入、步过和步出。
有关更多信息,请参阅VS Code文档中的“VS Code:调试”。
配置项目以进行调试
要为应用程序添加 Qt 调试配置:
- 转到“Run and Debug ”(在 macOS 上按Ctrl+Shift+D或Cmd+Shift+D)。

- 创建一个包含启动配置的launch.json文件 (1)。
- 选择“Add Configuration ”(2),然后选择与您的调试器匹配的“Qt: Debug ”调试配置。
- 勾选启动目标 (3)。若要更改,请选择目标名称,或前往“Command Palette ”并选择“CMake: Set Launch/Debug Target ”。
调试源代码
您还可以调试 Qt 源代码。但是,如果您使用 vcpkg 安装了 Qt,则 Qt Extension for VS Code 将无法找到源代码并会显示警告。要禁用该警告:
- 转到“Command Palette ”,然后选择“Open Qt settings ”。
- 选择“Qt C++ Configuration ”,然后选择“Do Not Warn Missing Source Dir ”。
Qt 调试配置
请选择以下其中一种 Qt 调试配置:
- Qt: Debug with cppdbg 用于使用 GDB 调试 C/C++ 应用程序。
- Qt: Debug with cppvsdbg (Windows) 用于使用 Visual Studio Windows 调试器调试 C/C++ 应用程序。
- Qt: QML: Attach by port 用于将 QML 调试器附加到正在运行的应用程序上以调试 QML 代码。
- Qt: QML: Launch 先启动应用程序,然后将 QML 调试器附加到该应用程序上以调试 QML 代码。
- Qt: Debug with cppdbg and QML debugger 用于使用 GDB 和 QML 调试器调试包含 C/C++ 代码的Qt Quick 应用程序。
- Qt: Debug with cppvsdbg and QML debugger (Windows) 使用 Visual Studio Windows 调试器和 QML 调试器调试包含 C/C++ 代码的Qt Quick 应用程序。
- Qt: PySide: Launch 使用
debugpy调试Qt for Python 应用程序。 - Qt: PySide: Launch with QML Debugger 用于使用
debugpy和 QML 调试器调试包含 C/C++ 代码的Qt for Python 和 QML 应用程序。 - Qt: Debug with lldb 用于使用 LLDB 进行调试。
- Qt: Debug Qt WASM with Chrome 使用 Chrome 调试 Qt WebAssembly 应用程序。
另请参阅 《调试Qt Quick 应用程序》、《调试Qt for Python 应用程序》、《调试 Qt WebAssembly 应用程序》以及《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.