本页内容

调试Qt for Python 应用程序

您可以使用debugpy和 QML 调试器来调试同时包含 Python 和 QML 代码的 Python 应用程序。

调试 Python 代码

若要仅调试应用程序中的Qt for Python 代码,请按照《VS Code:在VS Code中调试Python》中的说明进行操作。

要启动并调试Qt for Python 应用程序:

  1. 打开一个launch.json文件。有关更多信息,请参阅《调试 Qt 应用程序》。
  2. 选择“Python Debugger ”。
  3. 选择“Add Configuration ”,然后选择“Qt: PySide: Launch ”调试配置。

该配置如下所示:

"configurations": [
    Pyside
    {
        "name": "Python Debugger: Current File",
        "type": "debugpy",
        "request": "launch",
        "program": "${file}",
        "console": "integratedTerminal"
    },
    {
        "name": "Qt: PySide: Launch",
        "type": "debugpy",
        "request": "launch",
        "program": "${workspaceFolder}/main.py",
        "preLaunchTask": "PySide: build"
    }
]

注意:如果 `main.py ` 不是应用程序的入口点,请在 `"program"` 中进行修改。

调试 Python 和 QML 混合代码

您可以创建一个调试配置,该配置首先启动应用程序,然后使用“Qt: Acquire Port ”预启动任务获取的端口号,将debugpy 和QML调试器同时附加到该应用程序上。

启用 QML 调试

要启用 QML 调试,请在您的 QML 应用程序中添加以下代码:

from pathlib import Path
from argparse import ArgumentParser

from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine, QQmlDebuggingEnabler

if __name__ == "__main__":
    argument_parser = ArgumentParser()
    argument_parser.add_argument("-qmljsdebugger", action="store",
                                 help="Enable QML debugging")
    options = argument_parser.parse_args()
    if options.qmljsdebugger:
        QQmlDebuggingEnabler.enableDebugging(True)

    app = QApplication(sys.argv)
    engine = QQmlApplicationEngine()

启动和调试应用程序

QML 调试器会连接到由 QmlEngine 实例运行的套接字服务器。请在启动配置中指定端口号。

要调试同时包含 Python 和 QML 代码的Qt Quick 应用程序:

  1. 打开一个launch.json文件。有关详细信息,请参阅《调试 Qt 应用程序》。
  2. 选择“Python Debugger ”。
  3. 选择“Add Configuration ”,然后选择“Qt: PySide: Launch with QML Debugger ”调试配置。
  4. 在“args ”中,将“"port" ”的值设置为要使用的端口号:
    "configurations": [
        {
        "name": "Qt: PySide: Launch with QML debugger",
        "type": "debugpy",
        "request": "launch",
        "program": "${workspaceFolder}/main.py",
        "preLaunchTask": "PySide: build",
        "args": [
            "-qmljsdebugger=port:<port_number>,block,services:DebugMessages,QmlDebugger,V8Debugger"
    ]
  5. 选择“Add Configuration ”,然后选择“Qt: QML: Attach by port ”调试配置。
  6. 在launch.json文件中,将“port ”选项的值设置为该端口号:
    "configurations": [
        {
            "name": "Qt: QML: Attach by port",
            "type": "qml",
            "request": "attach",
            "host": "localhost",
            "port": "<port_number>"
        },
  7. 在launch.json文件中添加一个用于 Python/QML 调试的复合启动任务:
    "compounds": [
        {
            "name": "Python/QML",
            "configurations": [
                "Qt: PySide: Launch with QML debugger,
                "Qt: QML: Attach by port"
            ]
        }
    ]
  8. 运行该复合调试配置。

自动分配端口号

使用${command:qt-qml.debugPort} 变量以及复合启动任务和预启动任务来自动分配端口号:

"configurations": [
    {
        "name": "Qt: PySide: Launch with QML debugger",
        "type": "debugpy",
        "request": "launch",
        "program": "${workspaceFolder}/main.py",
        "preLaunchTask": "PySide: build",
        "args": [
            "-qmljsdebugger=port:${command:qt-qml.debugPort},block,services:DebugMessages,QmlDebugger,V8Debugger"
            ]
    },
    {
        "name": "Qt: QML: Attach by port",
        "type": "qml",
        "request": "attach",
        "host": "localhost",
        "port": "${command:qt-qml.debugPort}"
    },
],
"compounds": [
    {
        "name": "Python/QML",
        "configurations": [
            "Qt: PySide: Launch with QML debugger,
            "Qt: QML: Attach by port"
        ],
        "preLaunchTask": "Qt: Acquire Port",
    }
]

另请参阅 《调试 Qt 应用程序》、《调试Qt Quick 应用程序》、《VS Code:调试》以及《VS Code:Python 调试》。

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