QT从零入门教程(五):图像文件操作 [新建打开保存]

在学了在学了! 2020-08-19 18:32:47 2991

// 头文件

    #pragma once
    #include <QtWidgets/QMainWindow>
    #include <QtGui>
    #include <QtWidgets>
    #include <QMainWindow>
    #include "ui_mainWindow.h"

    class mainWindow : public QMainWindow
    {
        Q_OBJECT

    public:
        mainWindow(QWidget *parent = Q_NULLPTR);
        ~mainWindow();

    private:
        Ui::mainWindowClass ui;

        QDockWidget *dock_Image;                    // 图像窗口
        QString currentPath;                        // 当前图像的路径
        QLabel *imgLabel;                           // 图像显示框
        void InitImage();                           // 初始化图像
        void Menu_File();                           // 文件菜单

    private slots :
        void File_new();        // 新建
        void File_open();       // 打开
        void File_save();       // 保存
        void File_saveas();     // 另存为
                                // 关闭不需要,直接使用close()
    };
.

    // CPP
    #include "mainWindow.h"
    #include <QtGui>
    #include <QtWidgets>
    #include <QMainWindow>
    #pragma execution_character_set("utf-8")    

    mainWindow::mainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        ui.setupUi(this);

        Menu_File();        // 文件菜单
        InitImage();        // 初始化图像QLabel
    }

    mainWindow::~mainWindow()
    {
    }

    void mainWindow::Menu_File()        // 文件菜单
    {
        // 菜单栏
        QMenu *file = menuBar()->addMenu(tr("文件"));

        // 菜单动作
        QAction *Act_file_new = new QAction(QIcon("../Image/file/New.png"), tr("新建"), this);
        Act_file_new->setShortcuts(QKeySequence::New);  // 快捷键 Ctrl+N
        connect(Act_file_new, SIGNAL(triggered()), this, SLOT(File_new()));

        QAction *Act_file_open = new QAction(QIcon("../Image/file/Open.png"), tr("打开"), this);
        Act_file_open->setShortcuts(QKeySequence::Open);// 快捷键 Ctrl+O
        connect(Act_file_open, SIGNAL(triggered()), this, SLOT(File_open()));

        QAction *Act_file_save = new QAction(QIcon("../Image/file/Save.png"), tr("保存"), this);
        Act_file_save->setShortcuts(QKeySequence::Save);// 快捷键 Ctrl+S
        connect(Act_file_save, SIGNAL(triggered()), this, SLOT(File_save()));

        QAction *Act_file_saveas = new QAction(QIcon("../Image/file/SaveAs.png"), tr("另存为"), this);
        Act_file_saveas->setShortcuts(QKeySequence::SaveAs);// 快捷键 
        connect(Act_file_saveas, SIGNAL(triggered()), this, SLOT(File_saveas()));

        QAction *Act_file_close = new QAction(QIcon("../Image/file/Close.png"), tr("关闭"), this);
        Act_file_close->setShortcuts(QKeySequence::Close);// 快捷键 Ctrl+F4
        connect(Act_file_close, SIGNAL(triggered()), this, SLOT(close()));

        // 将动作添加到菜单上
        file->addAction(Act_file_new);
        file->addAction(Act_file_open);
        file->addAction(Act_file_save);
        file->addAction(Act_file_saveas);
        file->addSeparator();                       //添加分割线
        file->addAction(Act_file_close);

        // 工具栏
        ui.mainToolBar->addAction(Act_file_new);
        ui.mainToolBar->addAction(Act_file_open);
        ui.mainToolBar->addAction(Act_file_save);

        // 任务栏
        Act_file_new->setStatusTip(tr("新建图像"));
        Act_file_open->setStatusTip(tr("打开图像"));
        Act_file_save->setStatusTip(tr("保存图像"));
        Act_file_saveas->setStatusTip(tr("图像另存为"));
        Act_file_close->setStatusTip(tr("关闭软件"));
    }

    void mainWindow::InitImage()    // 初始化图像Label
    {
        //……
    }

    void mainWindow::File_new()     // 新建
    {
        //……
    }

    void mainWindow::File_open()    // 打开
    {
        //……
    }

    void mainWindow::File_save()    // 保存
    {
        //…… 
    }

    void mainWindow::File_saveas()  // 另存为
    {
        //……
    }

软件的运行结果如下:

运行无误,界面基本完成了。下面完善功能部分。

显示图像

这里运用QLabel来显示图像(头文件中定义了QLabel *imgLabel),另一个显示图像的方法是QPainter,在后面的博文中会接触到。


      void mainWindow::InitImage()        // 初始化图像
    {
        // 初始化QDockWidget.在以后会讲到,是可移动隐藏的小窗口,
        // 可以实现PS、VS停靠窗口的效果,目前只需了解
        dock_Image = new QDockWidget(tr("图像"), this);          //   图像
        setCentralWidget(dock_Image);

        // 初始化QLabel
        imgLabel = new QLabel(dock_Image);
        imgLabel->setScaledContents(true);  // 设置QLabel自动适应图像大小

        // 初始图像
        QImage image = QImage(500, 500, QImage::Format_RGB32);  // 新建图像
        image.fill(qRgb(255, 255, 255));                        // 全白
        imgLabel->setPixmap(QPixmap::fromImage(image));         // 显示图像
        imgLabel->resize(image.width(), image.height());        // 图像与imgLabel同大小

        // 增加滚动条,如果图像比imgLabel大,就会出现滚动条
        QScrollArea* scrollArea = new QScrollArea(this);
        scrollArea->setBackgroundRole(QPalette::Dark);
        scrollArea->setAlignment(Qt::AlignCenter);
        scrollArea->setWidget(imgLabel);
        dock_Image->setWidget(scrollArea);
    }

效果是这样的

实现功能

    void mainWindow::File_new()         // 新建
    {
        QImage image = QImage(500, 500, QImage::Format_RGB32);  // 创建长宽各500的RGB图像
        image.fill(qRgb(255, 255, 255));                        // 白色图像
        imgLabel->setPixmap(QPixmap::fromImage(image));         // 转载图像
        imgLabel->resize(image.width(), image.height());        // imgLabel与图像同大小
        currentPath = "";                                       // 当前路径为空
    }

    void mainWindow::File_open()        // 打开
    {   QString path = QFileDialog::getOpenFileName(this, tr("选择图像"), ".", tr("Images(*.jpg *.png *.bmp)"));                            // 文件选择框
        if (!path.isEmpty())                                    // 检测当前路径是否正确
        {
            QImage* img = new QImage();                     
            if (!(img->load(path)))
            {
                QMessageBox::information(this, tr("错误"), tr("打开图像失败!"));
                delete img;
                return;
            }
            imgLabel->setPixmap(QPixmap::fromImage(*img));
            imgLabel->resize(img->width(), img->height());
            currentPath = path;
        }
    }

    void mainWindow::File_save()        // 保存
    {
        if (currentPath.isEmpty())      // 判断是新建的图像还是打开的图像
        {
            QString path = QFileDialog::getSaveFileName(this, tr("保存图像"), ".", tr("Images(*.jpg *.png *.bmp)"));
            {
                if (!path.isEmpty())
                    currentPath = path;
            }
        }
        QImage img = imgLabel->pixmap()->toImage();     // 读取图像
        img.save(currentPath);                          // 保存图像
    }

    void mainWindow::File_saveas()      // 另存为
    {
        QString path = QFileDialog::getSaveFileName(this, tr("图像另存为"), ".", tr("Images(*.jpg *.png *.bmp)"));
        if (!path.isEmpty())
        {
            QImage img = imgLabel->pixmap()->toImage();
            img.save(path);
    currentPath = path;
        }
    }
![](https://ebaina.oss-cn-hangzhou.aliyuncs.com/res/images/202008/19/20200819-183230-881.png)

原文连接:https://blog.csdn.net/u013165921/article/details/79380097

声明:本文内容由易百纳平台入驻作者撰写,文章观点仅代表作者本人,不代表易百纳立场。如有内容侵权或者其他问题,请联系本站进行删除。
红包 点赞 收藏 评论 打赏
评论
0个
内容存在敏感词
手气红包
    易百纳技术社区暂无数据
相关专栏
置顶时间设置
结束时间
删除原因
  • 广告/SPAM
  • 恶意灌水
  • 违规内容
  • 文不对题
  • 重复发帖
打赏作者
易百纳技术社区
在学了在学了!
您的支持将鼓励我继续创作!
打赏金额:
¥1易百纳技术社区
¥5易百纳技术社区
¥10易百纳技术社区
¥50易百纳技术社区
¥100易百纳技术社区
支付方式:
微信支付
支付宝支付
易百纳技术社区微信支付
易百纳技术社区
打赏成功!

感谢您的打赏,如若您也想被打赏,可前往 发表专栏 哦~

举报反馈

举报类型

  • 内容涉黄/赌/毒
  • 内容侵权/抄袭
  • 政治相关
  • 涉嫌广告
  • 侮辱谩骂
  • 其他

详细说明

审核成功

发布时间设置
发布时间:
是否关联周任务-专栏模块

审核失败

失败原因
备注
拼手气红包 红包规则
祝福语
恭喜发财,大吉大利!
红包金额
红包最小金额不能低于5元
红包数量
红包数量范围10~50个
余额支付
当前余额:
可前往问答、专栏板块获取收益 去获取
取 消 确 定

小包子的红包

恭喜发财,大吉大利

已领取20/40,共1.6元 红包规则

    易百纳技术社区