This demo shows how to code and debug CMake-based Qt application in VSCode. It's actually quite simple.
Before compiling & running this demo, something should be configures:
- Set system environmental variable
Qt5_DIR
to be the absolute path of your Qt installation. - Install VSCode and relating extensions:
- CMake Tools
- C/C++ (It might be installed by default).
What I did including the following things:
-
Configure the global includePath of VSCode to support intelligence of Qt classes. This is done by configuring the VSCode's global setting file: settings.json by adding things below:
"[cpp]": { "editor.quickSuggestions": true }, "[c]": { "editor.quickSuggestions": true }, "C_Cpp.default.includePath": [ "${env:Qt5_DIR}/include", "${workspaceFolder}/src" ],
-
Configure the launch parameter of the project. This is done by editing the launch.json of the project:
"program": "${command:cmake.launchTargetPath}", "environment": [ {"name": "Path", "value": "${env:Path};${env:Qt5_DIR}/bin"} ],
${command:cmake.launchTargetPath}
will be evaluated to return the path of your CMake target. It avoids hard-coding the executable path. Andenvironment
adds the Qt's bin folder to the application's running path.