博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
QT5.9.1与opencv3.3读取图片(2)
阅读量:1887 次
发布时间:2019-04-26

本文共 3513 字,大约阅读时间需要 11 分钟。

我之前写过一篇,但是我们对于这种读取操作不是很满意,一般我们打开文件都是有一个对话框,并且图片是在一个小窗口中显示的。

关于如何新建一个项目,请看上一篇。

我们想要有一个小窗口去显示图片,那我们就要用到QMdiArea。从左边拖一个MDI Area到窗口中,点击MainWindow然后选择栅格布局。

我们看到MDI Area的边界和MainWindow的边界有一些距离,这很难看,所以我们要调整这个间隔。选中centralWidget,然后修改LayoutLeftMarginLayoutTopMarginLayoutRightMarginLayoutBottomMargin 都设置为0。

接着我们要对小窗口进行设计,由于我们要显示一个图片,最简单的做法就是新建一个ChildWindow类,让这类继承QLabel类就可以了。

我们现在来处理ChildWindow类,其中用到了我们之前写的一个转换函数,我这里新建一个头文件qimageandmat.h,将上面文章中给的代码copy过去。

//childwindow.cpp//读取图片bool ChildWindow::loadFile(const QString &fileName){    //读取一幅图片,这里我们要将QString转换为String使用toLocal8Bit()函数,避免乱码问题    cv::Mat image = cv::imread(fileName.toLocal8Bit().data());    if(!image.data)    {        QMessageBox::warning(this, tr("CVS"),                                 tr("Cannot read file %1\n")                                 .arg(fileName));//添加头文件#include 
return false; } else { //将Mat转化为QPixmap,使用了我们写的一个函数 QPixmap img = CVS::cvMatToQPixmap(image); //显示在label中 this->setPixmap(img); //改变label的大小用于自适应图像大小 this->setScaledContents(true); } setCurrentFile(fileName);//设置小窗口的标题 return true;}

接着编写setCurrentFile

//childwindow.cpp//设置小窗口的标题void ChildWindow::setCurrentFile(const QString &fileName){    curFile = QFileInfo(fileName).canonicalFilePath();//获取文件绝对路径,添加头文件#include 
setWindowTitle(userFriendlyCurrentFile() + "[*]");}QString ChildWindow::userFriendlyCurrentFile(){ return strippedName(curFile);}QString ChildWindow::strippedName(const QString &fullFileName){ return QFileInfo(fullFileName).fileName();}

最后ChildWindow的头文件片段

#include 
#include
#include
#include "qimageandmat.h"class ChildWindow : public QLabel{ Q_OBJECTpublic: ChildWindow(); bool loadFile(const QString &fileName); QString userFriendlyCurrentFile(); QString currentFile() { return curFile; }private: void setCurrentFile(const QString &fileName); QString strippedName(const QString &fullFileName); QString curFile;};

接着我们处理MainWindow

原来的mainwindow中的actionOpenFile槽函数这个时候变为

//mainwindow.cppvoid MainWindow::on_actionOpenFile_triggered(){    fileName = QFileDialog::getOpenFileName(this);  //文件打开窗口,添加头文件#include 
if (!fileName.isEmpty()) openFile(fileName);//打开文件}

接着编写openFile函数

//mainwindow.cpp//打开文件窗口bool MainWindow::openFile(const QString &fileName){    const bool succeeded = loadFile(fileName);  //读取文件    return succeeded;}

接着编写loadFile文件

//mainwindow.cpp//读取图片bool MainWindow::loadFile(const QString &fileName){    ChildWindow *child = createChildWindow();       //创建一个子窗口    const bool succeeded = child->loadFile(fileName);//子窗口读取图片    if (succeeded)                                //如果读取成功,显示图片;否则,关闭        child->show();    else        child->close();    return succeeded;}

接着编写createChildWindow函数

//mainwindow.cpp//创建子窗口ChildWindow *MainWindow::createChildWindow(){    ChildWindow *child = new ChildWindow;    ui->mdiArea->addSubWindow(child);    return child;}

最后MainWindow的头文件片段

#include 
#include
#include "childwindow.h"namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{ Q_OBJECTpublic: explicit MainWindow(QWidget *parent = 0); bool openFile(const QString &fileName); ~MainWindow();private slots: void on_actionOpenFile_triggered();private: bool loadFile(const QString &fileName); ChildWindow *createChildWindow(); QMdiArea *mdiArea; QString fileName; Ui::MainWindow *ui;};

接着运行

ok!^_^

转载地址:http://hqkdf.baihongyu.com/

你可能感兴趣的文章
开源项目|RT-Thread 软件包应用作品:小闹钟
查看>>
在 RT-Thread Studio 上使用 RT-Thread Nano
查看>>
开源项目|软件包应用作品:通用物联网系统平台
查看>>
【经验分享】RT-Thread UART设备驱动框架初体验(中断方式接收带\r\n的数据)
查看>>
单片机里面的CPU使用率是什么鬼?
查看>>
推荐一个优质Linux技术公众号-作者都是一线Linux代码贡献者们哦
查看>>
RT-Thread 编程风格指南
查看>>
95后高校电子教师,软硬兼修有趣有料!
查看>>
使用 STM32 通用 Bootloader ,让 OTA 更加 Easy
查看>>
Cache 的基本概念与工作原理
查看>>
装机量超亿台 RISC-V +IoT OS!中科蓝讯与RT-Thread战略合作,共推自主物联网生态发展
查看>>
Android程序员必备!面试一路绿灯Offer拿到手软,Android面试题及解析
查看>>
Android开发知识体系!腾讯+字节+阿里面经真题汇总,成功入职阿里
查看>>
typescript学习(进阶)
查看>>
三天敲一个前后端分离的员工管理系统
查看>>
EL表达式、JSTL标签库、文件上传和下载
查看>>
Cookie、Session
查看>>
表单重复提交
查看>>
Filter
查看>>
微服务架构实施原理详解
查看>>