VSCode环境配置(QT)

Visual Studio Code

1.QT安装

  1. 系统:win10

  2. 安装准备:官网下载qt-online-installer-windows-x64.exe在线安装程序

  3. QT账户申请个人免费版

  4. 选择版本:勾选Archive并筛选

  1. 选择对应组件

若是自己下载MinGW,需要使用posix版本,实测5.3、7.3和8.1posix版本均可使用,为了保持一致性,最好使用8.1版本

  1. 环境变量添加

若环境变量过长超过Windows预设长度可参考以下文章:

https://blog.csdn.net/mzl_18353516147/article/details/130387147 新建公共路径前缀or引入新的拓展环境变量

  1. 重启

环境变量设置可能不生效,建议重启电脑

2.插件安装

  1. CMake相关

  1. QT相关

  1. C++相关

3.Json配置

  1. CMake图形化配置

CMake侧边栏-->配置-->可选择对应工具包,这里选择GCC 8.1.0

若是系统环境较为混乱可以打开cmake-tools-kits.json文件(该文件位于user目录下,可以选择使用Everything软件进行系统检索),提前备份一份,再将cmake-tools-kits.json打开,将其中相关内容删去重新扫描工具包,此时会自动更新json文件

  1. sc文件夹下json配置
  • task.json

直接附上代码,修改command路径为QT对应位置即可

点击查看代码
{
    "tasks": [
        {
            "type": "shell",
            "label": "CMake Build",
            "command": "cmake",
            "args": [
                "--build",
                "${workspaceRoot}/build",
                "--config",
                "Debug"
            ],
            "group": "build",
            "problemMatcher": []
        },
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "D:/Software/QT/Tools/mingw810_64/bin/g++.exe",
            // "command": "D:\\Software\\Visual Studio Code2023\\MinGW\\x86_64-7.3.0-posix-sjlj\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}
  • settings.json

同理,修改compilerPath和miDebuggerPath路径为QT对应位置即可

点击查看代码
{
    "cmake.generator": "MinGW Makefiles",
    "cmake.buildDirectory": "${workspaceFolder}/build",
    "qtConfigure.qtKitDir": "D:/Software/QT/5.15.2/mingw81_64",
    "files.associations": {
        "qmainwindow": "cpp",
        "iosfwd": "cpp",
        "*.ipp": "cpp",
        "any": "cpp",
        "array": "cpp",
        "atomic": "cpp",
        "*.tcc": "cpp",
        "cctype": "cpp",
        "chrono": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "codecvt": "cpp",
        "condition_variable": "cpp",
        "csignal": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cstring": "cpp",
        "ctime": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "deque": "cpp",
        "list": "cpp",
        "unordered_map": "cpp",
        "vector": "cpp",
        "exception": "cpp",
        "algorithm": "cpp",
        "functional": "cpp",
        "iterator": "cpp",
        "map": "cpp",
        "memory": "cpp",
        "memory_resource": "cpp",
        "numeric": "cpp",
        "optional": "cpp",
        "random": "cpp",
        "ratio": "cpp",
        "source_location": "cpp",
        "string": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "utility": "cpp",
        "fstream": "cpp",
        "future": "cpp",
        "initializer_list": "cpp",
        "iomanip": "cpp",
        "iostream": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "mutex": "cpp",
        "new": "cpp",
        "ostream": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "thread": "cpp",
        "cinttypes": "cpp",
        "typeinfo": "cpp",
        "variant": "cpp",
        "qpointf": "cpp",
        "bit": "cpp",
        "charconv": "cpp",
        "compare": "cpp",
        "concepts": "cpp",
        "coroutine": "cpp",
        "resumable": "cpp",
        "format": "cpp",
        "forward_list": "cpp",
        "ios": "cpp",
        "locale": "cpp",
        "queue": "cpp",
        "stop_token": "cpp",
        "xfacet": "cpp",
        "xhash": "cpp",
        "xiosbase": "cpp",
        "xlocale": "cpp",
        "xlocbuf": "cpp",
        "xlocinfo": "cpp",
        "xlocmes": "cpp",
        "xlocmon": "cpp",
        "xlocnum": "cpp",
        "xloctime": "cpp",
        "xmemory": "cpp",
        "xstddef": "cpp",
        "xstring": "cpp",
        "xtr1common": "cpp",
        "xtree": "cpp",
        "xutility": "cpp",
        "qapplication": "cpp"
    },
    "C_Cpp.default.compilerPath": "D:/Software/QT/Tools/mingw810_64/bin/g++.exe",
    "cmake.debugConfig": {
        "miDebuggerPath": "D:/Software/QT/Tools/mingw810_64/bin/gdb.exe"
    },
    "cmake.ignoreKitEnv": true,
}
  • launch.json

同理,修改value和DebuggerPath路径为QT对应位置即可

点击查看代码
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Qt_debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${command:cmake.launchTargetPath}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": [
                {
                    "name": "PATH",
                    "value": "D:/Software/QT/5.15.2/mingw81_64/bin"
                }
            ],
            "externalConsole": false,
            "miDebuggerPath": "D:/Software/QT/Tools/mingw810_64/bin/gdb.exe",
        {
            "name": "test_debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/build/module/api/test.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": [
                {
                    "name": "PATH",
                    "value": "D:/Software/QT/5.15.2/mingw81_64/bin"
                }
            ],
            "externalConsole": false,
            "miDebuggerPath": "D:/Software/QT/Tools/mingw810_64/bin/gdb.exe",
        }
    ]
}
posted @ 2024-08-01 21:12  Little_R  阅读(2116)  评论(0)    收藏  举报