Qt for Python アプリケーションのデバッグ
Python と QML の両方のコードを含む Python アプリケーションを、debugpyと QML デバッガを使ってデバッグすることができます。
Python コードのデバッグ
アプリケーションのQt for Python コードだけをデバッグするには、VS Code の指示に従ってください:VSコードでのPythonデバッグ。
Qt for Python アプリケーションを起動してデバッグするには
- launch.jsonファイルを開きます。詳細については、Qt アプリケーションのデバッグを参照してください。
- Python Debugger を選択します。
- 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 アプリケーションをデバッグするには、次のようにします:
- launch.jsonファイルを開いてください。詳細については、Qtアプリケーションのデバッグを参照してください。
- Python Debugger を選択します。
- Add Configuration を選択し、Qt: PySide: Launch with QML Debugger デバッグ設定を選択します。
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" ]- Add Configuration を選択し、Qt: QML: Attach by port デバッグ構成を選択します。
- launch.jsonファイルの
portオプションの値としてポート番号を設定します:"configurations": [ { "name": "Qt: QML: Attach by port", "type": "qml", "request": "attach", "host": "localhost", "port": "<port_number>" }, - launch.jsonファイルに、Python/QMLデバッグ用の複合起動タスクを追加します:
"compounds": [ { "name": "Python/QML", "configurations": [ "Qt: PySide: Launch with QML debugger, "Qt: QML: Attach by port" ] } ] - 複合デバッグ設定を実行する。
ポート番号を自動的に割り当てる
ポート番号を自動的に割り当てるには、${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:デバッグ]も参照してください :Qt アプリケーションのデバッグ」、「 アプリケーションのデバッグ」、「VS Code:デバッグ」、「VS Code:Python debugging in VS Code」も参照してください。
© 2024 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.