搭建 QT6+OpenCv4.7+CMake的环境
本文主要介绍如何搭建QT6+OpenCv的开发环境,基本流程如下
- 先安装CMake3.27.3,用来编译适用用QT的OpenCv的源码,安装完成后要配置系统的环境变量
- 安装Qt6的开发环境,并配置环境变量,注意一定要重启电脑才能生效
- 下载OpenCv的源码,用于后面的编译
- 上述环境准备好后,使用CMake来编译适配Qt的OpenCv源码
- 安装OpenCv
版本详情:QT6.4.3 + OpenCv4.7 + CMake 3.27.3
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434543-1991372325.png)
CMake的安装
- CMake 的安装比较简单,一直按照默认设置点下去就可以,在下面的页面时,选择添加CMake到系统路径。
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434995-12725244.png)
- 安装完成后的文件如下
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434906-230641874.png)
OpenCv的安装
- 双击安装包,选择解压路径
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434787-51877097.png)
- 解压过程
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434929-511347646.png)
- 解压完成后文件如下:
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434964-793805505.png)
Qt的安装
- 一定要注意安装路径不要有空格
- 双击安装包,然后一路点击“下一步”
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081435135-452093060.png)
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081437014-258599196.png)
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434953-1895310775.png)
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081435170-141294041.png)
- 选择自定义安装
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081435099-992434648.png)
- 选择Qt
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081436864-24862712.png)
- 选择Developer and Designer Tools
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081436969-2105737061.png)
- 一路点击,继续安装
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081437240-1342797425.png)
- 安装进行中,这个过程比较长,需要先下载后安装
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081437164-1460456746.png)
配置系统环境变量
- 按照自己的安装路径,配置系统环境变量,配置完成后,一定要重启电脑
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434942-335778066.png)
基于CMake 配置OpenCv的编译环境
- 在opencv文件夹下新建rebuild_for_qt文件夹,存放新编译的内容
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434985-266606000.png)
- 打开CMake
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434990-258957331.png)
- 配置如下,点击Config
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081435099-1405958503.png)
- 在出现的界面进行如下配置
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434969-1927728607.png)
- 在出现的界面,选择安装qt时的安装的gcc.exe 和g++
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434831-1505995260.png)
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434639-836403705.png)
- 点击Finished,进行配置
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081436920-548134163.png)
- 配置完成之后,会有一些错误,然后进行选择后,再重新配置
- 选中“WITH_OPENGL”、“WITH_QT”,WITH_OPENMP,
- 将“WITH_IPP”取消,以及带有Python的配置取消
- 然后再次点【Configure】
- 再次Config后,依然会出现错误,主要是不能从github下载一些文件
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434965-1311103722.png)
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434977-161051060.png)
- 找到源码目录下的.cache\ffmpeg文件夹,把压缩包里的3个文件改为和下面文件名称一致
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434635-640573866.png)
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434959-1351862252.png)
- 再次Config后,就只剩下warning,不再有错误了,此时表示构建完成,然后点击Generate
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081435224-586855644.png)
- 出现如下信息,表示构建完成
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434923-895803177.png)
编译OpeCv
- 在 rebuild_for_qt目录下打开命令行cmd,输入mingw32-make -j 2 的指令,注意 j 后面的2指的是自己电脑的内核数,这条指令的意思是创建2个线程来编译OpenCv, 可以根据自己电脑的配置来修改这个数字,按下回车开始编译。
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081437337-1978341521.png)
- 全部编译完成到100%,没有红色错误提示即可
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081435154-479713702.png)
- 输入mingw32-make install 命令,进行安装,安装完成后如下
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081435110-598518565.png)
- OpenCv安装完成后,要将其配置到系统环境中
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081435014-144116665.png)
使用Qt测试环境配置是否成功
- 创建一个Qt的项目
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081435158-2023916233.png)
- 填写项目名称和存储路径
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434795-472876654.png)
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434839-86773238.png)
- 打开.pro文件 配置头文件和库目录的引用
INCLUDEPATH += E:\opencv_4.7\rebuild_for_qt\install\include LIBS += E:\opencv_4.7\rebuild_for_qt\lib\libopencv_*.a
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434873-1837092291.png)
- 测试代码,加载并显示一张图片
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081434873-342264444.png)
- 编译并运行项目,图片成功显示,表示配置成功!
![0](https://img2023.cnblogs.com/blog/635893/202309/635893-20230912081436961-2142534816.png)