You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Qt VS Tools创建Qt项目时读取VS项目设置报错

Qt VS Tools新建项目报错解决方案

问题描述

使用Visual Studio 2022通过Qt VS Tools模板创建Qt项目时,触发两个异常:

  1. QtVSException: Error reading VS project settings
  2. ArgumentException: An item with the same key has already been added

异常堆栈跟踪

QtVSException堆栈:

Stack Trace:
   at QtVsTools.Core.VersionInformation..ctor(String qtDirIn)
   at QtVsTools.Core.VersionInformation.Get(String qtDir)
   at QtVsTools.Core.QtVersionManager.GetVersionInfo(String name)
   at QtVsTools.Wizards.ProjectWizard.ConfigPage..ctor()
   at QtVsTools.Wizards.ProjectWizard.ConsoleWizard.<get_WizardWindow>b__8_1()
   at QtVsTools.Common.LazyFactory.Get[T](Expression`1 propertyRef, Func`1 initFunc)
   at QtVsTools.Wizards.ProjectWizard.ProjectTemplateWizard.RunStarted(Object 
       automationObject, Dictionary`2 parameterValues, WizardRunKind runKind, Object[] 
       customParams)

ArgumentException堆栈:

Stack Trace:
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource 
    resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, 
    TValue value, Boolean add)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement] 
   (IEnumerable`1 source, Func`2 keySelector, Func`2 
    elementSelector, 
    IEqualityComparer`1 comparer)
   at QtVsTools.Core.HelperFunctions.SetVcVars(VersionInformation 
    versionInfo, ProcessStartInfo startInfo)
   at QtVsTools.Core.QMakeImport.QMakeProcess.Run()

环境版本

  • Qt 5.12.12(msvc2017_x64)
  • Visual Studio 2022 17.8.4
  • Qt VS Tools 2022 3.0.2/3.0.1(均已尝试)

已尝试的无效操作

  • 重新安装Visual Studio、Qt及Qt VS Tools
  • 手动设置QtMsBuild和QTDIR环境变量
  • 尝试Stack Overflow相关方案

解决方案

1. 清理重复环境变量

  • 打开系统属性的环境变量设置,检查用户变量和系统变量中:
    • 删除Path里重复的Qt路径、VC++工具链路径
    • 删除重复的QTDIR、QtMsBuild变量,确保仅保留一组正确配置
    • QtMsBuild需指向Qt VS Tools安装目录下的MsBuild文件夹,示例路径:C:\Program Files (x86)\Microsoft Visual Studio\2022\Community\MSBuild\Qt\QtMsBuild(根据你的VS版本和安装路径调整)

2. 重置Qt VS Tools配置与缓存

  • 打开Visual Studio,依次点击扩展 > Qt VS Tools > Qt Options,删除所有已配置的Qt版本
  • 重新添加Qt 5.12.12的msvc2017_x64目录,确认路径无空格、特殊字符
  • 关闭Visual Studio,删除以下缓存文件:
    • 删除%APPDATA%\QtVsTools目录下所有内容
    • 删除%LOCALAPPDATA%\Microsoft\VisualStudio\17.0_xxxx\ComponentModelCache目录下的文件(xxxx为你的VS实例ID)

3. 修复VC++工具链环境变量冲突

  • 打开Developer Command Prompt for VS 2022,执行vcvars64.bat
  • 输入set命令,查看INCLUDE、LIB等变量是否存在重复路径
  • 若存在重复,删除系统环境变量中手动添加的VC++路径,让VS自动配置工具链环境

4. 验证Qt安装完整性

  • 进入Qt5.12.12\msvc2017_x64\bin目录,执行qmake -v,确认能正常输出版本信息
  • 若qmake运行异常,重新安装Qt 5.12.12的msvc2017_x64组件,选择完整安装包

5. 降级Qt VS Tools版本

  • 卸载当前3.0.x版本的Qt VS Tools,安装2.12.0版本(该版本对Qt 5.12和VS 2022兼容性更稳定)
  • 重新配置Qt版本后尝试新建项目

内容的提问来源于stack exchange,提问作者jack.py

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 10:52:02