Visual Studio Code开发Qt应用程序实现自动提示语法高亮代码导航编译和调试

Posted 岬淢箫声

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Visual Studio Code开发Qt应用程序实现自动提示语法高亮代码导航编译和调试相关的知识,希望对你有一定的参考价值。

准备开发软件

以下简称Visual Studio Code为VSCode。

前言

Visual Studio在安装Qt插件后虽然能进行Qt应用开发,但是它本身的体积太大,微软还没有开放Visual Studio单独安装C++开发包的方法。Qt Creator 大版本号仍然停留在4.3上,某些功能迟迟等不来更新和实现,特别是——调试功能。在非用户代码中出现的ASSERT错误,QT Creator不告诉用户代码堆栈,只显示第三方代码哪里ASSERT,让人无语。虽然UI开发、SIGNAL-SLOT的设计仍然由Qt Creator借力,但其它开发工作在VS Code上进行会更让用户省心省力。本文将重点介绍如何用VS Code开发Qt5应用程序。

VSCode本身没有针对Qt应用开发的插件程序,开发工作进行之前务必确保C++开发功能正常。关于VSCode C++开发环境的配置请参考:《VSCode实现C++代码提示、语法高亮、编译和调试

开发环境

  1. Windows Software Development Kit 10.0.15063
  2. Microsoft Visual C++ Build Tools 2015
  3. Microsoft Visual Studio Code
  4. Qt 5.9.1 for Windows
  5. Cppcheck 1.8
    • Cppcheck 1.8的下载地址是:http://cppcheck.sourceforge.net/。这个工具对于C++代码检查、代码提示非常有帮助。安装过程结束后把工具的安装位置加入Path环境变量。
  6. LLVM 5.0
    • LLVM 5.0的下载地址是:http://releases.llvm.org/download.html。LLVM自带的CLang工具对代码自动格式化非常有帮助。此外LLVM也是一款很好用的编译套件,以后也用的上。安装过程结束后把工具的安装位置加入Path环境变量。
  7. CMake 3.9
    • CMake 3.9的下载地址是:https://cmake.org/download/。CMake是一套自动编译工具,它提供可视化的图形界面,自动生成适用于各种编译平台的Makefile。VS Code的C++编译套件对它有依赖。虽然下文用的是NMake,但CMake留着也不错。安装过程结束后把工具的安装位置加入Path环境变量。

安装顺序按照以上列出软件的顺序进行。

Demo

VSCode开发Qt应用程序比开发单纯的C++程序更简单,因为Qt会自己生成Makefile,免去了用户编译Makefile的烦恼。

命令提示符

Qt所有的平台都有一个类似于Visual Studio中vcvars.bat这样的批处理文件,叫做qtenv2.bat。它的作用是把当前平台的bin目录加入到应用程序的Path环境变量。打开一个简单的Qt编译命令窗口的代码很简单,代码如下:

@echo off
title qmake and nmake build prompt
set VCINSTALLDIR=C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC
set QTDIR=C:\\Qt\\Qt5.9.1\\5.9.1\\msvc2015_64
set PATH=%VCINSTALLDIR%\\bin;%QTDIR%\\bin;C:\\Program Files\\7-Zip;%PATH%
call "%VCINSTALLDIR%\\vcvarsall.bat" amd64
cmd /k

新建工程

模板选择是Qt Widgets Application,如下图所示:

输入工程名称demo1,如下图所示:

Kit Selection去掉其它所有的选择,只留下MSVC2015 X64,如下图所示:

不用输入什么,进入下一步,如下图所示:

暂不启用版本控制,如下图所示:

创建成功。如下图所示:

用VSCode打开工程文件夹,如下图所示:

自动提示与代码导航

在.vscode文件夹下新建c_cpp_properties.json,代码如下:


    "version": 3,
    "configurations": [
        "name": "msvc2015-x64",
        "intelliSenseMode": "msvc-x64",
        "includePath": [
            "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DAnimation",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DCore",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DExtras",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DInput",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DLogic",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DQuick",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DQuickAnimation",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DQuickExtras",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DQuickInput",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DQuickRender",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DQuickScene2D",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DRender",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtAccessibilitySupport",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtANGLE",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtBluetooth",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtCharts",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtConcurrent",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtCore",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtDataVisualization",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtDeviceDiscoverySupport",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtEglSupport",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtEventDispatcherSupport",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtFbSupport",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtFontDatabaseSupport",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtGui",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtHelp",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtLocation",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtMultimedia",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtMultimediaQuick_p",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtMultimediaWidgets",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtNetwork",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtNetworkAuth",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtNfc",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtOpenGL",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtOpenGLExtensions",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtPacketProtocol",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtPlatformCompositorSupport",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtPlatformHeaders",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtPositioning",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtPrintSupport",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtPurchasing",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQml",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQmlDebug",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQuick",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQuickControls2",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQuickParticles",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQuickTemplates2",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQuickTest",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQuickWidgets",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtRemoteObjects",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtRepParser",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtScxml",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtSensors",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtSql",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtSvg",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtTest",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtThemeSupport",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtUiPlugin",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtUiTools",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtWebChannel",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtWebEngineCore",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtWebEngineWidgets",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtWebSockets",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtWebView",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtWidgets",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtXml",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtXmlPatterns",
            "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtZlib",
            "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/shared",
            "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/ucrt",
            "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/um",
            "$workspaceRoot"
        ],
        "browse": 
            "path": [
                "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DAnimation",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DCore",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DExtras",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DInput",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DLogic",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DQuick",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DQuickAnimation",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DQuickExtras",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DQuickInput",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DQuickRender",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DQuickScene2D",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DRender",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtAccessibilitySupport",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtANGLE",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtBluetooth",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtCharts",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtConcurrent",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtCore",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtDataVisualization",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtDeviceDiscoverySupport",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtEglSupport",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtEventDispatcherSupport",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtFbSupport",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtFontDatabaseSupport",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtGui",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtHelp",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtLocation",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtMultimedia",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtMultimediaQuick_p",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtMultimediaWidgets",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtNetwork",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtNetworkAuth",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtNfc",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtOpenGL",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtOpenGLExtensions",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtPacketProtocol",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtPlatformCompositorSupport",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtPlatformHeaders",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtPositioning",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtPrintSupport",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtPurchasing",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQml",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQmlDebug",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQuick",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQuickControls2",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQuickParticles",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQuickTemplates2",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQuickTest",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQuickWidgets",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtRemoteObjects",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtRepParser",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtScxml",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtSensors",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtSql",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtSvg",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtTest",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtThemeSupport",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtUiPlugin",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtUiTools",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtWebChannel",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtWebEngineCore",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtWebEngineWidgets",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtWebSockets",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtWebView",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtWidgets",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtXml",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtXmlPatterns",
                "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtZlib",
                "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/shared",
                "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/ucrt",
                "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/um",
                "$workspaceRoot"
            ]
        
    ]

修改settings.json,确保文件里面至少有以下内容:

"cppcheck.includePaths": [
        "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DAnimation",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DCore",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DExtras",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DInput",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DLogic",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DQuick",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DQuickAnimation",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DQuickExtras",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DQuickInput",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DQuickRender",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DQuickScene2D",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/Qt3DRender",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtAccessibilitySupport",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtANGLE",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtBluetooth",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtCharts",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtConcurrent",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtCore",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtDataVisualization",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtDeviceDiscoverySupport",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtEglSupport",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtEventDispatcherSupport",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtFbSupport",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtFontDatabaseSupport",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtGui",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtHelp",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtLocation",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtMultimedia",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtMultimediaQuick_p",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtMultimediaWidgets",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtNetwork",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtNetworkAuth",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtNfc",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtOpenGL",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtOpenGLExtensions",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtPacketProtocol",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtPlatformCompositorSupport",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtPlatformHeaders",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtPositioning",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtPrintSupport",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtPurchasing",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQml",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQmlDebug",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQuick",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQuickControls2",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQuickParticles",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQuickTemplates2",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQuickTest",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtQuickWidgets",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtRemoteObjects",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtRepParser",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtScxml",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtSensors",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtSql",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtSvg",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtTest",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtThemeSupport",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtUiPlugin",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtUiTools",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtWebChannel",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtWebEngineCore",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtWebEngineWidgets",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtWebSockets",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtWebView",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtWidgets",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtXml",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtXmlPatterns",
        "C:/Qt/Qt5.9.1/5.9.1/msvc2015_64/include/QtZlib",
        "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/shared",
        "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/ucrt",
        "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/um",
        "$workspaceRoot"
    ],
    "clang-format.executable": "C:/Program Files/LLVM/bin/clang-format.exe",
    "files.associations": 
        "qjsonobject": "cpp",
        "ostream": "cpp",
        "qwebenginepage": "cpp",
        "vector": "cpp",
        "*.rh": "cpp",
        "qwidget": "cpp",
        "system_error": "cpp",
        "xlocale": "cpp",
        "qwebenginehistory": "cpp",
        "iosfwd": "cpp",
        "xstring": "cpp",
        "xutility": "cpp"
    ,
    "files.exclude": 
        "**/.git": true,
        "**/.svn": true,
        "**/.hg": true,
        "**/CVS": true,
        "**/.DS_Store": true,
        "**/GPATH": true,
        "**/GRTAGS": true,
        "**/GTAGS": true,
        "**/*.user": true,
        "**/Makefile*": true,
        "**/debug": true,
        "**/release": true,
        "**/Debug": true,
        "**/Release": true,
        "**/*.exe": true,
        "**/*.dll": true,
        "**/*.stash": true,
        "**/*.bat": true,
        "**/*.cmd": false,
        "**/*cache": true,
        "**/*.sdf": true,
        "**/*.suo": true,
        "**/Win32": true,
        "**/ipch": true,
        "**/x64": true,
        "**/GeneratedFiles": true,
        "**/build": true
    

以上配置成功后自动提示、语法高亮、代码导航等功能可以正常工作。

编译

修改默认的编译任务,即tasks.json,代码如下:


    "version": "2.0.0",
    "tasks": [
        "taskName": "demo1 Debug",
        "type": "shell",
        "command": "cmd",
        "args": [
            "/c",
            "$workspaceRoot/build.cmd",
            "debug"
        ],
        "group": 
            "kind": "build",
            "isDefault": true
        
    ]

新建build.cmd,代码如下:

@echo off
title qmake and nmake build prompt
set VCINSTALLDIR=C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC
set QTDIR=C:\\Qt\\Qt5.9.1\\5.9.1\\msvc2015_64
set PATH=%VCINSTALLDIR%\\bin;%QTDIR%\\bin;C:\\Program Files\\7-Zip;%PATH%
call "%VCINSTALLDIR%\\vcvarsall.bat" amd64
qmake demo1.pro -spec win32-msvc "CONFIG+=debug" "CONFIG+=qml_debug"
nmake
cd debug
windeployqt demo1.exe

调试

新建launch.json,代码如下:


    "version": "0.2.0",
    "configurations": [
        "name": "CDB",
        "type": "cppvsdbg",
        "request": "launch",
        "program": "demo1.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "$workspaceRoot/debug/",
        "environment": [],
        "externalConsole": true
    ]

试试效果

VSCode调试功能强于Qt,Qt UI设计功能又强于VSCode,两者互补,给我们工作带来不少方便。以上配置完成后就可以尝到C++与Qt联合工作的甜头了。

编译效果

按下Ctrl+Shilt+B试试,截图如下:

调试效果

写两个局部变量看看:

    int a=1;
    int b=2;
    int c=a+b;
    qDebug()<<a<<"+"<<b<<"="<<c;

先下一个断点,如下图所示:

按下F5,如下图所示:

代码导航效果

代码编辑器已经能够导航了,如下图所示:

至此,Visual Studio Code开发Qt应用程序实现自动提示、语法高亮、代码导航、编译和调试工作全部成功完成。

以上是关于Visual Studio Code开发Qt应用程序实现自动提示语法高亮代码导航编译和调试的主要内容,如果未能解决你的问题,请参考以下文章

沙龙预告在Visual Studio Code下开发Angular应用

visual studio 和visual studio code 的区别

开发环境安装 Visual Studio Code 开发环境 ( 下载 Visual Studio Code 安装器 | Visual Studio Code )

《软件工具》手把手教你使用Visual Studio Code开发C/C++(Windows)

《软件工具》手把手教你使用Visual Studio Code开发C/C++(Linux)

visual studio 和visual studio code 的区别是啥