cmake编译colmap报错QT Could not find the Qt platform plugin "windows" in ""

1.问题

使用CMake编译colmap,编译完成后,在colmap生成的exe目录中powershell运行“colmap gui”打开GUI界面,报错

E:\PyProjects\Demo\colmap\build\src\colmap\exe\Release>colmap.exe gui 
qt.qpa.plugin: Could not find the Qt platform plugin "windows" in "" 
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fi x this problem.

CMake版本 4.2.0

colmap版本 3.14.0 dev

电脑安装的QT版本 5.15.2

qwindows.dll 存在

2.原因

2.1 chatgpt解释

这是 Windows 下典型的 Qt 运行时插件路径问题,与 COLMAP 是否成功编译本身无关,而是 Qt 平台插件(platforms/windows.dll)在运行时未被正确找到。当前的错误信息已经非常明确地指向了这一点。

(1)启用 Qt 插件调试

Release 目录,执行:

$env:QT_DEBUG_PLUGINS = "1"
.\colmap.exe gui

输出

(base) PS E:\PyProjects\Demo\colmap\build\src\colmap\exe\Release> colmap gui
qt.qpa.plugin: Could not find the Qt platform plugin "windows" in "C:\Qt\5.15.2\msvc2019_64\plugins"
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fi

Qt 平台插件被找到了,但被强制从错误的 Qt 运行时加载,导致 ABI 不一致而加载失败。

(2)彻底离开 Conda(不是 deactivate)

不要在 (base) 里再试了。

  1. 关闭当前 PowerShell

  2. 打开 普通 PowerShell(开始菜单 → Windows PowerShell)

  3. 确认提示符 没有 (base)

PS: 关闭 base 自动激活

在 任意 PowerShell(即使是 base) 中执行:

conda config --set auto_activate_base false

(3)清除所有 Qt 相关环境变量

Remove-Item Env:QT_PLUGIN_PATH -ErrorAction SilentlyContinue
Remove-Item Env:QT_QPA_PLATFORM_PLUGIN_PATH -ErrorAction SilentlyContinue

然后显式指定只用本地 plugins

$env:QT_QPA_PLATFORM_PLUGIN_PATH = "$PWD\platforms"

(4)用“最干净 PATH”启动

Release 目录执行:

cmd /c "set PATH=%CD% && colmap.exe gui"

然后,依然失败了

2.2 实际原因

在使用cmake的gui程序编译的时候,QT的路径已经被默认设置QT6相关,例如

QT6_DIR  E:/PyProjects/Demo/colmap/build/vcpkg_installed/x64-windows/share/Qt6
QT6_core_dir
QR6gui_dir
QT6opengl_dir
...

应该修改值为安装的QT5路径

qt6core_DIRqt6gui_DIRqt6opengl_DIR 等条目,都是 CMake 在扫描 vcpkg Qt6 时生成的条目,它们会干扰 COLMAP 的 Qt5 配置。

(1)vcpkg 默认行为

  1. build 中使用了 vcpkg 作为依赖管理工具。

  2. 当前 vcpkg 安装的是 Qt6(路径里 /share/Qt6),而不是 Qt5。

  3. CMake 在扫描依赖时,会优先读取 vcpkg 提供的 Qt6 配置,所以在 GUI 里显示 Qt6_DIR

(2)为什么没有 Qt5_DIR

  • vcpkg 中如果没有安装 qt5,只有 qt6,CMake 就不会显示 Qt5_DIR,只会列出 Qt6_DIR

  • 本地安装的 Qt5(C:/Qt/5.15.2/msvc2019_64)和 vcpkg 的 Qt6 是 不同来源,CMake GUI 默认不会自动使用你系统 Qt5,除非你手动指定。

(3)影响

 

  • 如果 CMake 使用 vcpkg 提供的 Qt6,会尝试生成 Qt6 版本的 COLMAP

 

 

  • 当前系统里 只有 Qt5 GUI,而 COLMAP 5.15.2 是和 Qt5 配套的,Qt6 会导致:

 

 

    ①找不到 qwindows 平台插件

 

 

    ②编译出的 GUI 不兼容或无法启动

 

这也解释了之前反复出现:

qt.qpa.plugin: Could not find the Qt platform plugin "windows"

因为 CMake 误用 Qt6 路径生成了不可用的 GUI。

3.解决方法

在 CMake GUI 中设置:

 
选项说明
CMAKE_BUILD_TYPE Release Release 编译
Qt5_DIR C:/Qt/5.15.2/msvc2019_64/lib/cmake/Qt5 Qt CMake 配置路径,类型为path
BUILD_GUI ON 一定要启用 GUI
BUILD_TESTS OFF 可选,节省编译时间
BUILD_EXAMPLES OFF 可选
BUILD_APPS ON 构建可执行文件

最重要的就是设置QT5_DIR的值。

并且,删除所有QT6相关的路径的选项以及值。

4.后续问题1 仍然无法打开colmap的gui程序

4.1 问题

使用powershell启动

& "C:/Qt/5.15.2/msvc2019_64/bin/windeployqt.exe" ".\colmap.exe"

报错

PS E:\PyProjects\Demo\colmap\build\src\colmap\exe\Release> & "C:/Qt/5.15.2/msvc2019_64/bin/windeployqt.exe" ".\colmap.exe"
Unable to find dependent libraries of D:\ProgramData\anaconda3\Library\bin\Qt5Widgets.dll :Cannot open 'D:/ProgramData/anaconda3/Library/bin/Qt5Widgets.dll':

说明 windeployqt 找到的 Qt5 DLL 不是本地安装的 Qt5,而是 Anaconda 自带的 Qt5,导致它去 Anaconda 目录查找依赖,但无法打开。

4.2 原因

 

(1)系统环境变量的 PATH 中有 Anaconda 路径,而且 Anaconda 自带 Qt5(D:/ProgramData/anaconda3/Library/bin)。

 

 

(2)windeployqt 会先查找 PATH 中的 Qt DLL,然后去解析依赖库,所以被 Anaconda 的 Qt5 干扰。

 

 

(3)这就是之前反复遇到 “qt.qpa.plugin: Could not find the Qt platform plugin "windows"” 的根源之一。

 

4.3 解决方法

不扰动原有conda环境的解决方法:

(1)临时启动干净的 PowerShell

①打开一个 全新的 PowerShell(不要通过 Anaconda Prompt 启动)。

②在这个窗口中设置 PATH 仅包含本地 Qt5 bin

$env:PATH = "C:/Qt/5.15.2/msvc2019_64/bin"

③运行 windeployqt

& "C:/Qt/5.15.2/msvc2019_64/bin/windeployqt.exe" "E:/PyProjects/Demo/colmap/build/src/colmap/exe/Release/colmap.exe"

这样 windeployqt 会只使用 本地 Qt5 DLL,不会碰到 Anaconda 的 Qt5。

完成后关闭这个 PowerShell,不会影响其他窗口或 Conda 环境。

5.后续问题2 打开其他依赖QT的软件(itwin capture master)报错

5.1 问题

打开之前安装的软件(itwin capture master) 报错:

This application failed to start because no Qt platform plugin could beinitialized. Reinstalling the application may fix this problem. Available platform plugins are: direct2d, minimal, offscreen, windows. 

经典错误了。

5.2 原因

chatgpt解释

该错误并不是 iTwin Capture Master 本身损坏,而是你安装/编译 Qt5(用于 COLMAP)之后,破坏了系统中 Qt 运行时环境的优先级,导致 iTwin 误加载了“错误版本的 Qt 插件”,最终找不到或无法初始化正确的 qwindows.dll 平台插件。

(1)错误本质

错误信息核心是:

no Qt platform plugin could be initialized
Available platform plugins: windows, direct2d, minimal, offscreen

这说明:

  1. Qt 插件目录被找到了

  2. 但插件版本与 Qt Core(Qt5Core.dll)不匹配

  3. iTwin Capture Master 被迫加载了你为 COLMAP 安装的 Qt

而 iTwin 自带的是 它私有的 Qt 运行时不允许被外部 Qt 覆盖

(2)根本原因

你做了以下至少一件事:

  • 安装了 Qt5(Qt Online Installer / vcpkg / 手动)

  • 并且:

    • Qt/5.x.x/msvc*/bin

    • vcpkg/installed/x64-windows/bin

  • 加入了系统 PATH

所有 Qt 程序都会优先加载这个 Qt,而不是自己的

5.3 解决方法

删除系统变量的PATH中有关QT的路径

 

 

解决!