Windows下使用 VS Code + g++ 开发 Qt GUI 项目的完整指南

微软技术分享 微软全球最有价值专家
全栈领域优质创作者
博客专家认证
2025-04-25 08:53:31

🚀 使用 VS Code + g++ 开发 Qt GUI 项目的完整指南(Windows + MSYS2)

本指南帮助你在 Windows 下使用 VS Code + g++ + CMake + Qt6 快速搭建 Qt GUI 项目,适合熟悉 Visual Studio 的开发者向跨平台 VS Code 工具链迁移。


🛠️ 一、环境准备

1. 安装 MSYS2

  


#### 2. 安装 Qt 和工具链(在 MinGW64 终端中):


```bash
pacman -S \
  mingw-w64-x86_64-toolchain \
  mingw-w64-x86_64-cmake \
  mingw-w64-x86_64-ninja \
  mingw-w64-x86_64-qt6-base \
  mingw-w64-x86_64-qt6-tools \
  mingw-w64-x86_64-qt6-declarative \
  mingw-w64-x86_64-qt6-svg \
  mingw-w64-x86_64-qt6-shadertools \
  mingw-w64-x86_64-qt6-multimedia \
  mingw-w64-x86_64-qt6-translations

📁 二、项目目录结构

QtGuiApp/
│
├── .vscode/
│   ├── c_cpp_properties.json     # IntelliSense
│   ├── tasks.json                # 构建任务
│   ├── launch.json               # 调试配置
│
├── build/                        # 构建输出目录
├── CMakeLists.txt                # CMake 配置
├── main.cpp                      # 主程序
├── mainwindow.cpp / .h / .ui     # Qt 主窗口

⚙️ 三、CMake 配置 CMakeLists.txt

cmake_minimum_required(VERSION 3.16)
project(QtGuiApp LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

find_package(Qt6 REQUIRED COMPONENTS Widgets)

add_executable(QtGuiApp
    main.cpp
    mainwindow.cpp
    mainwindow.h
    mainwindow.ui
)

target_link_libraries(QtGuiApp PRIVATE Qt6::Widgets)

💡 四、VS Code 配置

1. IntelliSense .vscode/c_cpp_properties.json

{
  "configurations": [
    {
      "name": "Win32",
      "includePath": [
        "${workspaceFolder}/**",
        "C:/msys64/mingw64/include",
        "C:/msys64/mingw64/include/c++/14.2.0",
        "C:/msys64/mingw64/include/c++/14.2.0/x86_64-w64-mingw32",
        "C:/msys64/mingw64/include/Qt6",
        "C:/msys64/mingw64/include/Qt6/QtWidgets",
        "C:/msys64/mingw64/include/Qt6/QtCore"
      ],
      "defines": ["_DEBUG", "UNICODE"],
      "compilerPath": "C:/msys64/mingw64/bin/g++.exe",
      "cStandard": "c17",
      "cppStandard": "c++17",
      "intelliSenseMode": "windows-gcc-x64"
    }
  ],
  "version": 4
}

2. 构建与运行任务 .vscode/tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "CMake Configure",
      "type": "shell",
      "command": "cmake -S . -B build -G Ninja",
      "group": "build"
    },
    {
      "label": "CMake Build",
      "type": "shell",
      "command": "cmake --build build",
      "group": "build",
      "dependsOn": ["CMake Configure"]
    },
    {
      "label": "Run Executable",
      "type": "shell",
      "command": ".\\build\\QtGuiApp.exe",
      "group": {
        "kind": "test",
        "isDefault": true
      },
      "dependsOn": ["CMake Build"]
    }
  ]
}

3. 调试配置 .vscode/launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug Qt App",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/build/QtGuiApp.exe",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}/build",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "miDebuggerPath": "C:/msys64/mingw64/bin/gdb.exe",
      "setupCommands": [
        {
          "description": "Enable pretty-printing",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }
  ]
}

📦 五、使用 windeployqt 打包 Qt GUI 应用

在 MSYS2 MinGW64 终端中,使用以下命令将 Qt 依赖复制到可执行文件目录:

cd build
windeployqt QtGuiApp.exe

该命令会自动将 Qt DLL、平台插件、SVG 模块等复制至当前目录,生成可独立运行的 .exe 包。

该命令会自动将 Qt DLL、平台插件、SVG 模块等复制至当前目录,生成可独立运行的 .exe 包。


🔍 六、Visual Studio 与 VS Code 使用差异对比

特性Visual StudioVS Code + g++ + CMake
工程结构.sln,.vcxproj图形化工程CMakeLists.txt构建系统
编译器MSVCg++ (MinGW64 from MSYS2)
构建方式菜单或按钮自动构建tasks.json自定义任务/命令行构建
调试器内建 VS 调试器gdb +launch.json配置调试
跨平台Windows 为主跨平台:Windows / Linux / macOS
UI 设计工具Qt Designer 插件或内嵌独立 Qt Designer(.ui文件)
特性Visual StudioVS Code + g++ + CMake工程结构.sln, .vcxproj 图形化工程CMakeLists.txt 构建系统编译器MSVCg++ (MinGW64 from MSYS2)构建方式菜单或按钮自动构建tasks.json 自定义任务/命令行构建调试器内建 VS 调试器gdb + launch.json 配置调试跨平台Windows 为主跨平台:Windows / Linux / macOSUI 设计工具Qt Designer 插件或内嵌独立 Qt Designer(.ui 文件)

✅ 附:检查 g++ 支持的 C++ 标准版本

g++ -std=c++17 -dM -E - < nul | findstr __cplusplus


文章来源: https://blog.csdn.net/weixin_44455665/article/details/147488051
版权声明: 本文为博主原创文章,遵循CC 4.0 BY-SA 知识共享协议,转载请附上原文出处链接和本声明。


...全文
235 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

5,185

社区成员

发帖
与我相关
我的任务
社区描述
微软技术社区为中国的开发者们提供一个技术干货传播平台,传递微软全球的技术和产品最新动态,分享各大技术方向的学习资源,同时也涵盖针对不同行业和场景的实践案例,希望可以全方位地帮助你获取更多知识和技能。
windowsmicrosoft 企业社区
社区管理员
  • 山月照空舟
  • 郑子铭
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

微软技术社区为中国的开发者们提供一个技术干货传播平台,传递微软全球的技术和产品最新动态,分享各大技术方向的学习资源,同时也涵盖针对不同行业和场景的实践案例,希望可以全方位地帮助你获取更多知识和技能。

予力众生,成就不凡!微软致力于用技术改变世界,助力企业实现数字化转型。

试试用AI创作助手写篇文章吧