参考一个博主的步骤:Qt6MinGW+OpenCV+CMake+Windows11环境搭建详细记录_qt6安装opencv-CSDN博客
链接放在这里了
QT6默认大家都已经安装好了,没安装的可以去搜索QT6安装的教程
1.安装opencv:
安装:
我安装的opencv默认在C盘,根据自己安装的路径找到opencv目录下的sources:
在opencv目录下新建一个:opencv-build文件夹,用来存放编译之后的文件
2.配置环境变量:非常重要,必须要做!
配置完环境变量之后,重启电脑
3.cmake
点击:Configure
点击Configure
开始配置:
展开WITH
,勾选WITH_QT
和WITH_OPENGL
再次点击Configure
,报红就再点一次,然后点击Generate
PS:
这里如果自己选错了,可以点这个清除:
4.编译:
输入mingw32-make.exe -j 8
再输入:mingw32-make.exe install
把这个文件夹的路径添加到系统变量
5.测试:
新建一个QT项目:
pro文件下:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
//增加这两行,根据自己的路径更改:
INCLUDEPATH +=C:\\opencv\\opencv-build\\install\\include
LIBS += C:\\opencv\\opencv-build\\installx64\mingw\\bin\\libopencv_*.dll
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
cpp文件下:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//图片路径
cv::Mat image = cv::imread("C:\\Users\\Administrator\\Desktop\\Snipaste_2024-08-07_14-34-55.png", 1);
cv::namedWindow("Test");
cv::imshow("Test", image);
}
MainWindow::~MainWindow()
{
delete ui;
}
测试结果:
标签:QT6,QT,配置,MainWindow,opencv,ui,include,mainwindow From: https://blog.csdn.net/2303_82327196/article/details/141092218