Vs2022+QT+Opencv 一些需要注意的地方

要在vs2022创建QT项目,先要安装一个插件Qt Visual Studio Tools,根据个人经验选择LEGACY Qt Visual Studio Tools好一些,看以下内容之前建议先在vs2022中配置好opencv,配置方式建议以属性表的形式保存在硬盘上。

 设置QT路径

 

打开vs2022 ,选择Qt Widgets Application

 

这里不清楚为什么只能选择一个,就默认Next吧

 点Finish后如下图所示,双击ui文件

修改头文件,增加2行,

private slots:
    void on_buttonOpen_clicked();

以及修改最后1行如图 

Ui::QtWidgetsApplication1Class *ui;

 

修改主文件cpp之前,先加载配置好的opencv属性表(这个是vs2022中配置opencv的问题,这里不详细展开,主要讲vs中qt的使用)

 

opencv的属性表加载之后,就可以调用opencv功能了,QtWidgetsApplication1.cpp头部增加以下代码

 修改后的内容如下

#include "qtpro2.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QPixmap>

#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;
using namespace std;

qtpro2::qtpro2(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::qtpro2Class)
{
    //ui.setupUi(this);
	ui->setupUi(this);
}

给打开图片这个按钮加上代码,功能是读取一张图片,并等比例(不变形)缩放后显示在Qlabel中

 

Mat img_input;
void QtWidgetsApplication1::on_pushButton_clicked()
{

    QString filename = QFileDialog::getOpenFileName(this, "打开图像文件", "C:/Users", "Image Files (*.bmp;*.png;*.jpg)");

    if (filename.isEmpty()) {
        QMessageBox::information(this, "提示", "文件打开失败1!");
        return;
    }

    img_input = cv::imread(filename.toLocal8Bit().toStdString());

    if (img_input.empty()) {

        QMessageBox::information(this, "提示", "文件打开失败2!");
        return;
    }

    cv::Mat temp;
    cv::cvtColor(img_input, temp, cv::COLOR_BGR2RGB);

    // 直接将文件名传递给 QPixmap 进行加载
    QPixmap pixmap = QPixmap::fromImage(QImage((const unsigned char*)(temp.data), temp.cols, temp.rows, temp.step, QImage::Format_RGB888));

    if (pixmap.isNull()) {
        QMessageBox::information(this, "提示", "文件打开失败2!");
        return;
    }

    // 获取 label 的尺寸
    int labelWidth = ui->labelPic->width();
    int labelHeight = ui->labelPic->height();

    // 将 QPixmap 进行缩放
    QPixmap scaledPixmap = pixmap.scaled(labelWidth, labelHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);

    // 将缩放后的图像设置到 label 中
    ui->labelPic->setPixmap(scaledPixmap);
    ui->labelPic->setScaledContents(false); 
    ui->labelPic->setAlignment(Qt::AlignCenter); 

}

 到这一步,基本功能就完成,可以先生成项目,再运行看下

运行之后 ,可以看到可以选择图片了,不过窗口标题中文乱码

没意外的话,图片会显示在Label中,现在处理下标题乱码问题

 .

保存之后,要重新生成,再运行,我这里是生效了

 至此,vs20022中使用qt调用opencv,从0到1的跨越应该是成功了,后面如果时间允许,再加一些图片处理的功能,像锐化,素描化并保存的功能,希望对新手有点点帮助哈

QtWidgetsApplication1.cpp 全部代码

#include "QtWidgetsApplication1.h"

#include <QFileDialog>
#include <QMessageBox>
#include <QPixmap>

#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;
using namespace std;


QtWidgetsApplication1::QtWidgetsApplication1(QWidget *parent)
    : QMainWindow(parent) ,ui(new Ui::QtWidgetsApplication1Class)
{
    //ui.setupUi(this);//修改前
    ui->setupUi(this);//修改后
}

QtWidgetsApplication1::~QtWidgetsApplication1()
{}


Mat img_input;
void QtWidgetsApplication1::on_pushButton_clicked()
{

    QString filename = QFileDialog::getOpenFileName(this, "打开图像文件", "C:/Users", "Image Files (*.bmp;*.png;*.jpg)");

    if (filename.isEmpty()) {
        QMessageBox::information(this, "提示", "文件打开失败1!");
        return;
    }

    img_input = cv::imread(filename.toLocal8Bit().toStdString());

    if (img_input.empty()) {

        QMessageBox::information(this, "提示", "文件打开失败2!");
        return;
    }

    cv::Mat temp;
    cv::cvtColor(img_input, temp, cv::COLOR_BGR2RGB);

    // 直接将文件名传递给 QPixmap 进行加载
    QPixmap pixmap = QPixmap::fromImage(QImage((const unsigned char*)(temp.data), temp.cols, temp.rows, temp.step, QImage::Format_RGB888));

    if (pixmap.isNull()) {
        QMessageBox::information(this, "提示", "文件打开失败2!");
        return;
    }

    // 获取 label 的尺寸
    int labelWidth = ui->labelPic->width();
    int labelHeight = ui->labelPic->height();

    // 将 QPixmap 进行缩放
    QPixmap scaledPixmap = pixmap.scaled(labelWidth, labelHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);

    // 将缩放后的图像设置到 label 中
    ui->labelPic->setPixmap(scaledPixmap);
    ui->labelPic->setScaledContents(false); 
    ui->labelPic->setAlignment(Qt::AlignCenter); 

}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
VS2019中使用QtOpenCV进行打包时,首先需要确保已安装了QtOpenCV,并将它们与VS2019集成。 1. 打开VS2019,创建一个Qt应用程序项目。选择“文件”->“新建”->“项目”,然后选择“Qt应用程序”模板。 2. 在项目设置中,确保正确配置QtOpenCV。选择“项目”->“属性”,在属性对话框中,选择“Qt项目设置”并确保已选择正确的Qt版本。然后,在“VC++目录”下的“包含目录”和“库目录”中,添加OpenCV的包含和库目录。 3. 在项目文件中,包含OpenCV和相关的头文件,以便在代码中使用OpenCV的功能。比如,使用`#include <opencv2/opencv.hpp>`来包含OpenCV的头文件。 4. 开始编写和调试代码。可以使用OpenCV的各种功能来处理图像和视频等任务,并使用Qt的界面设计功能来创建用户界面。 5. 调试和完成代码后,可以开始打包应用程序。选择“生成”->“生成解决方案”,或者按下F7键进行编译。确保没有错误和警告。 6. 打包应用程序时,可以选择发布版本或调试版本。选择“生成”->“生成解决方案”后,可以在项目目录下的“发布”文件夹中找到生成的可执行文件。 7. 如果需要将其分发给其他人使用,还可以通过使用相关的安装程序打包应用程序。选择“生成”->“生成解决方案”后,可以在项目目录下的“发行”文件夹中找到打包应用程序的相关文件。 以上是使用VS2019进行QtOpenCV打包的简要步骤。根据具体的需求和应用场景,可能还需要进行其他配置和功能实现。如果遇到任何问题或困难,可以查阅相关文档和论坛,或者向开发者社区寻求帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

养牛大人

感谢您的鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值