このページについて

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