1. 程式人生 > 其它 >vs2019 配置 qt 庫 Visual Studio+Qt配置開發環境

vs2019 配置 qt 庫 Visual Studio+Qt配置開發環境

如標題所說,在 vs 編譯器中呼叫 qt 庫

首先安裝一個適合 vs 的 qt 版本

不知道怎麼安裝的,可以參考:Visual Studio+Qt配置開發環境

qt 安裝之後,我們只需要在 vs 編譯器中設定 qt 的標頭檔案目錄和庫目錄,比如

 輸入這些路徑時,需要設定 QtDir 巨集,我們可以直接在 vs 專案中的 .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>

如果你想在 vs 中開啟 qt 專案並編譯修改的話,可以在 VS 編譯器中安裝 QT VS Tools 拓展

如何安裝的,請參考:Qt:03---Visual Studio安裝Qt與使用


筆記:

今天檢視 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,重新編譯,沒有錯誤