VS2019 配置 Qt 库

如标题所说,在 VS 编译器中调用 Qt 库

首先安装一个适合 VS 的 Qt  版本

不知道怎么安装的,可以参考:Visual Studio+Qt配置开发环境

Qt 安装之后,需要在 VS 编译器中安装 Qt VS Tools 拓展,以便在 vs 中新建 C++/Qt 项目

不知道如何安装的,可以参考:Qt:03---Visual Studio安装Qt与使用

点击 Qt Versions 选项 add new Qt versions,可以按照下图来设置

我有两个 Qt 版本,这边可以任选一个,当然也只允许选一个

配置完 Qt 拓展之后,就可以新建一个项目了,比如 Qt Empty Application 或者 Qt Widgets Application 或者 Qt Console Application,根据自己的需求来建

接着在 vs 编译器中设置 Qt 的头文件目录和库目录,比如

 

 

 

 输入这些路径时,需要设置 QtDir 宏,我们可以直接在 VS 项目中的 .vcxproj.user 文件中手动输入配置就行(有时不需要,可以观察设置 QtDir 是否被编译器解释),比如

没有被正确解释的话就可以手动修改 .vcxproj.user 文件了

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <ShowAllFiles>false</ShowAllFiles>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <QTDIR>D:\Qt\5.15.0\msvc2019_x86_static</QTDIR>
    <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
    <QmlDebugSettings>file:$(ProjectGuid),block</QmlDebugSettings>
    <QmlDebug>-qmljsdebugger=$(QmlDebugSettings)</QmlDebug>
    <LocalDebuggerCommandArguments>$(QmlDebug)</LocalDebuggerCommandArguments>
    <LocalDebuggerEnvironment>PATH=$(QTDIR)\bin%3b$(PATH) $(LocalDebuggerEnvironment)</LocalDebuggerEnvironment>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DebugWin|Win32'">
    <QTDIR>D:\Qt\5.15.0\msvc2019_x86_static</QTDIR>
    <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
    <QmlDebugSettings>file:$(ProjectGuid),block</QmlDebugSettings>
    <QmlDebug>-qmljsdebugger=$(QmlDebugSettings)</QmlDebug>
    <LocalDebuggerCommandArguments>$(QmlDebug)</LocalDebuggerCommandArguments>
    <LocalDebuggerEnvironment>PATH=$(QTDIR)\bin%3b$(PATH) $(LocalDebuggerEnvironment)</LocalDebuggerEnvironment>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DebugDll|Win32'">
    <QTDIR>D:\Qt\5.15.0\msvc2019_x86_static</QTDIR>
    <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
    <QmlDebugSettings>file:$(ProjectGuid),block</QmlDebugSettings>
    <QmlDebug>-qmljsdebugger=$(QmlDebugSettings)</QmlDebug>
    <LocalDebuggerCommandArguments>$(QmlDebug)</LocalDebuggerCommandArguments>
    <LocalDebuggerEnvironment>PATH=$(QTDIR)\bin%3b$(PATH) $(LocalDebuggerEnvironment)</LocalDebuggerEnvironment>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    <QTDIR>D:\Qt\5.15.0\msvc2019_x86_static</QTDIR>
    <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
    <LocalDebuggerEnvironment>PATH=$(QTDIR)\bin%3b$(PATH) $(LocalDebuggerEnvironment)</LocalDebuggerEnvironment>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseDll|Win32'">
    <QTDIR>D:\Qt\5.15.0\msvc2019_x86_static</QTDIR>
    <LocalDebuggerEnvironment>PATH=$(QTDIR)\bin%3b$(PATH)</LocalDebuggerEnvironment>
  </PropertyGroup>
</Project>

笔记:

1. 今天查看 Qt VS Tools 时,发现该选项后面提示 qt visual studio tools version 2.9.1 initializing(rev.6),查看相关文章发现要更新 vs 编译器(我的是 vs2019),更新完后要重启电脑,

之后重新打开 VS 项目(里面包含 Qt 界面库的调用),编译后报错,提示 error MSB4184: 无法计算表达式“[System.IO.File]... qt.natvis.xml not found,这个错误之前是没有的,研究后发现是编译器没有找到该文件,

解决方法是,复制一个已有的 qt5.natvis.xml 文件,并重名为 qt.natvis.xml,重新编译,没有错误

2. 另外编译时会出现 error LNK2038: 检测到 “RuntimeLibrary” 的不匹配项的错误,这边的解决方法是将 多线程调试Dll (/MDd) 改为 多线程调试(/MTd) 

有些情况是反过来,可以都试一下

3. 编辑环境变量应该是需要的,可以根据需要来设置

设置完之后可以重启电脑或者 cmd 输入 set PATH=C; 来激活,并另外打开 cmd 输入 echo %PATH% 检查是否生效

一些细节可以参考:

补充:

Qt 的一些库需要依赖 win32 的库,所以在静态绑定库时,不仅要包括 Qt 库,还需要 win32 库,下面列举一些常用的库

#pragma comment(lib, "Dwmapi.lib")
#pragma comment(lib, "Winspool.lib")
#pragma comment(lib, "Imm32.lib")
#pragma comment(lib, "oleaut32.lib")
#pragma comment(lib, "shlwapi.lib")
#pragma comment(lib, "rpcrt4.lib")
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "comdlg32.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "wldap32.lib")
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "uuid.lib")
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "mpr.lib")
#pragma comment(lib, "version.lib")
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "uxtheme.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "crypt32.lib")
#pragma comment(lib, "strmiids.lib")
#pragma comment(lib, "dnsapi.lib")
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "D3d9.lib")
#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "userenv.lib")
#pragma comment(lib, "netapi32.lib")
#pragma comment(lib, "wtsapi32.lib")
#pragma comment(lib, "dwrite.lib")
#pragma comment(lib, "d2d1.lib")

// qwidget.h 支持
#ifdef _DEBUG
#pragma comment(lib, "Qt5Widgetsd.lib")
#else
#pragma comment(lib, "Qt5Widgets.lib")
#endif  // _DEBUG

// 平台插件支持
#ifdef _DEBUG
#pragma comment(lib, "qwindowsd.lib")
#pragma comment(lib, "Qt5EventDispatcherSupportd.lib")
#pragma comment(lib, "Qt5FontDatabaseSupportd.lib")
#pragma comment(lib, "qtfreetyped.lib")
#pragma comment(lib, "Qt5ThemeSupportd.lib")
#pragma comment(lib, "Qt5AccessibilitySupportd.lib")
#pragma comment(lib, "Qt5WindowsUIAutomationSupportd.lib")
#pragma comment(lib, "Qt5Guid.lib")
#pragma comment(lib, "qtlibjpegd.lib")
#pragma comment(lib, "qtlibpngd.lib")
#pragma comment(lib, "qtharfbuzzd.lib")
#pragma comment(lib, "Qt5Cored.lib")
#pragma comment(lib, "qtpcre2d.lib")
#pragma comment(lib, "libEGLd.lib")
#pragma comment(lib, "libGLESv2d.lib")
#else
#pragma comment(lib, "qwindows.lib")
#pragma comment(lib, "Qt5EventDispatcherSupport.lib")
#pragma comment(lib, "Qt5FontDatabaseSupport.lib")
#pragma comment(lib, "qtfreetype.lib")
#pragma comment(lib, "Qt5ThemeSupport.lib")
#pragma comment(lib, "Qt5AccessibilitySupport.lib")
#pragma comment(lib, "Qt5WindowsUIAutomationSupport.lib")
#pragma comment(lib, "Qt5Gui.lib")
#pragma comment(lib, "qtlibjpeg.lib")
#pragma comment(lib, "qtlibpng.lib")
#pragma comment(lib, "qtharfbuzz.lib")
#pragma comment(lib, "Qt5Core.lib")
#pragma comment(lib, "qtpcre2.lib")
#pragma comment(lib, "libEGL.lib")
#pragma comment(lib, "libGLESv2.lib")
#endif  // _DEBUG

// 图片插件支持
#ifdef _DEBUG
#pragma comment(lib, "qgifd.lib")
#pragma comment(lib, "qicnsd.lib")
#pragma comment(lib, "qicod.lib")
#pragma comment(lib, "qjpegd.lib")
#pragma comment(lib, "qsvgd.lib")
#pragma comment(lib, "Qt5Svgd.lib")  // QSvgPlugin 支持
#pragma comment(lib, "qtgad.lib")
#pragma comment(lib, "qtiffd.lib")
#pragma comment(lib, "qwbmpd.lib")
#pragma comment(lib, "qwebpd.lib")
#else
#pragma comment(lib, "qgif.lib")
#pragma comment(lib, "qicns.lib")
#pragma comment(lib, "qico.lib")
#pragma comment(lib, "qjpeg.lib")
#pragma comment(lib, "qsvg.lib")
#pragma comment(lib, "Qt5Svg.lib")  // QSvgPlugin 支持
#pragma comment(lib, "qtga.lib")
#pragma comment(lib, "qtiff.lib")
#pragma comment(lib, "qwbmp.lib")
#pragma comment(lib, "qwebp.lib")
#endif  // _DEBUG

// 音频插件支持
#ifdef _DEBUG
#pragma comment(lib, "qtaudio_windowsd.lib")
#pragma comment(lib, "Qt5Multimediad.lib")
#else
#pragma comment(lib, "qtaudio_windows.lib")
#pragma comment(lib, "Qt5Multimedia.lib")
#endif  // _DEBUG

// 窗体主题
#ifdef _DEBUG
#pragma comment(lib, "qwindowsvistastyled.lib")
#else
#pragma comment(lib, "qwindowsvistastyle.lib")
#endif  // _DEBUG

  

 

posted @ 2022-11-29 18:58  strive-sun  阅读(2919)  评论(0编辑  收藏  举报