Qt编写的简易版图片浏览器

UI展示

在这里插入图片描述

功能概述

一、图片导入:图片导入方式分为两种,QAction打开导入与拖拽导入;追加导入时界面新追加打开列表中的首张图片;
二、信息显示:导入图片列表后,页面中心的label中默认显示导入列表中的第一张图片和其全路径信息以及它在导入的所有图片中的位置序号信息,页面底部的listWidget中显示导入的所有图片的缩略图及其全路径名称;
三、图片操作:
1、切换操作:图片切换方式分两种,点击上一张下一张按钮切换与点击底部列表中条目进行切换,切换后,将在页面的label中显示图片及其相关信息;

在这里插入图片描述

2、缩放操作:点击工具栏或菜单栏下拉列表中对应的QAction,使用Qt中QTransform进行缩放操作,缩放时模糊度较大,质量较差;

在这里插入图片描述

3、旋转操作:点击工具栏或菜单栏下拉列表中对应的QAction,默认的顺转与逆转均为90度;

在这里插入图片描述

4、其它说明:保存与帮助按钮仅放置在工具条,未有任何实现,退出按钮实现退出程序;

程序源码:

头文件
#ifndef PICOPERATE_H
#define PICOPERATE_H

#include <QMainWindow>
#include <QStringList>
#include <QImage>
#include <QPixmap>
#include <QMimeData>
#include <QDropEvent>
#include <QDragEnterEvent>
#include <QWheelEvent>
#include <QUrl>

namespace Ui {
class PicOperate;
}

class PicOperate : public QMainWindow
{
    Q_OBJECT

public:
    explicit PicOperate(QWidget *parent = 0);
    ~PicOperate();
    void connectionSlots();
    void laodLabelPic(QString fillAllPath);
    void adaptPicLabelShow(QImage img);
    void loadSwitchPic(int index);
    void pinInfoShow(QString picRoad, int curIndex, int totalNum = 0);
    void tipInformation(QString tipStr);
protected:
    void dragEnterEvent(QDragEnterEvent *event);
    void dropEvent(QDropEvent *dropedEvent);
    void wheelEvent(QWheelEvent *event);
private slots:
    void listWidgetRowChanged(int currentRow);

    void openFileTriggered();
    void deletePicTriggered();

    void zoomInTriggered();
    void zoomOutTriggered();

    void upPicToolBtnClicked();
    void downPicToolBtnClicked();

    void leftRotateTriggered();
    void rightRotateTriggered();
private:
    QString picFileRoad;
    QStringList fullPathOfFiles;

    int picNumber;
    bool fileDirOpen;
    QImage zoomImg; // 用于旋转和放大缩小

    int picIndex;   // 用于图片切换
    int picImportIndex = 0;
private:
    Ui::PicOperate *ui;
};

#endif // PICOPERATE_H

源文件
#include "picoperate.h"
#include "ui_picoperate.h"
#include <QFileDialog>
#include <QListWidget>
#include <QPushButton>
#include <QMessageBox>
#include <QTransform>
#include <QList>
#include <QAction>
#include <QDir>
#include <QDebug>

#define ISNULL(IMG) if(IMG.isNull()){return;}
constexpr int rotateAngle = 90;

PicOperate::PicOperate(QWidget *parent) :
    QMainWindow(parent)
    , picNumber(0)
    , fileDirOpen(false)
    , picIndex(0)
    , ui(new Ui::PicOperate)
{
    ui->setupUi(this);
    this->setWindowTitle(tr("图片修改工具"));
    this->setAcceptDrops(true);

    connectionSlots();  // 连接动作与槽
}

PicOperate::~PicOperate()
{
    delete ui;
}

void PicOperate::connectionSlots()
{
    connect(ui->action_OpenFile, &QAction::triggered, this, &PicOperate::openFileTriggered);
    connect(ui->action_ZoomIn, &QAction::triggered, this, &PicOperate::zoomInTriggered);
    connect(ui->action_ZoomOut, &QAction::triggered, this, &PicOperate::zoomOutTriggered);
    connect(ui->action_Delete, &QAction::triggered, this, &PicOperate::deletePicTriggered);
    connect(ui->action_LeftRotate, &QAction::triggered, this, &PicOperate::leftRotateTriggered);
    connect(ui->action_RightRotate, &QAction::triggered, this, &PicOperate::rightRotateTriggered);
    connect(ui->action_Exit, &QAction::triggered, this, []{exit(0);});
    connect(ui->upPicToolButton, &QPushButton::clicked, this, &PicOperate::upPicToolBtnClicked);
    connect(ui->downPicToolButton, &QPushButton::clicked, this, &PicOperate::downPicToolBtnClicked);
    connect(ui->PiclistWidget, &QListWidget::currentRowChanged, this, &PicOperate::listWidgetRowChanged);
}

// 将新打开的文件夹中第一张图片显示在Qlabel中
void PicOperate::laodLabelPic(QString fillAllPath)
{
    QImage imgTemp;
    fileDirOpen = true; //  判断是否有图片文件载入
    imgTemp.load(fillAllPath);
    zoomImg.load(fillAllPath);
    ISNULL(imgTemp)
    adaptPicLabelShow(imgTemp);
    pinInfoShow(fillAllPath, picImportIndex, picNumber);
    picImportIndex = picNumber;   // 再次打开文件夹或拖拽导入时更新首张显示
}

// 图片尺寸大于label尺寸,初始显示调整为与label尺寸一致;小与label尺寸,显示图片本身尺寸
void PicOperate::adaptPicLabelShow(QImage img)
{
    int fitWidth = img.width() <= ui->PicShowLabel->width() ? img.width() : ui->PicShowLabel->width();
    int fitHeight = img.height() <= ui->PicShowLabel->height() ?  img.height() : ui->PicShowLabel->height();
    ui->PicShowLabel->setPixmap(QPixmap::fromImage(img.scaled(fitWidth, fitHeight)));
}

// 点击上一张,下一张按钮时加载图片至PicShowLabel
void PicOperate::loadSwitchPic(int index)
{
    QImage img;
    QString picName = fullPathOfFiles.at(index);
    zoomImg.load(picName);
    img.load(picName);
    ISNULL(img)
    adaptPicLabelShow(img);
    pinInfoShow(picName, index, picNumber);
    ui->PiclistWidget->setCurrentRow(index);
}

// 图片信息显示
void PicOperate::pinInfoShow(QString picRoad, int curIndex, int totalNum)
{
    QString PicInfoLabel = "当前文件名:" + picRoad;
    PicInfoLabel = PicInfoLabel + "   " + "第" + QString::number(curIndex + 1) + "/" + QString::number(totalNum) + "张";
    ui->picInfolabel->setText(PicInfoLabel);
}

// 提示信息框
void PicOperate::tipInformation(QString tipStr)
{
    QMessageBox::warning(this,tr("文件提示:"), tipStr, QMessageBox::Ok);
}

void PicOperate::dragEnterEvent(QDragEnterEvent *event)
{
    event->mimeData()->hasUrls() ? event->acceptProposedAction()
                                 : event->ignore();
}

 // 拖拽导入文件
void PicOperate::dropEvent(QDropEvent *dropedEvent)
{
    if (dropedEvent->mimeData()->hasUrls()) {
        QList<QUrl> urlList = dropedEvent->mimeData()->urls();
        for (int i = 0; i < urlList.size(); i++) {
            QString curFile = urlList.at(i).toLocalFile();
            picNumber == 0 ? fullPathOfFiles.insert(i, curFile)
                           : fullPathOfFiles.insert(picNumber + i, curFile);
            QListWidgetItem *signalPicItem = new QListWidgetItem(QIcon(curFile), curFile);
            ui->PiclistWidget->addItem(signalPicItem);
        }
        ui->PiclistWidget->setCurrentRow(picNumber);
        picNumber += urlList.size();
        laodLabelPic(urlList.at(0).toLocalFile());
    }
}

// Ctrl+滚轮控制图片缩放
void PicOperate::wheelEvent(QWheelEvent *event)
{
    if (QApplication::keyboardModifiers() == Qt::ControlModifier) {
        if (event->angleDelta().y() > 0) {
            zoomInTriggered();
        } else {
            zoomOutTriggered();
        }
    }
}

// 打开图片所在文件夹,并将其中所有图片文件显示在QListWidget中
void PicOperate::openFileTriggered()
{
    picFileRoad = QFileDialog::getExistingDirectory(this, "Open Dir", "../");
    if (picFileRoad.isEmpty()) {    // 用户选择打开文件夹后又取消
        return;
    }
    QDir openPicDir(picFileRoad);
    QStringList filter;
    filter<<"*.jpg"<<"*.png"<<"*.bmp"<<"*.jpeg"<<"*.ppm"<<
            "*.JPG"<<"*.PNG"<<"*.JPEG";
    openPicDir.setNameFilters(filter);

    QStringList picFileList = openPicDir.entryList(filter);
    for(int i = 0; i < picFileList.size(); i++) {
        QString picFullDir = picFileRoad + "/" + picFileList.at(i);
        picNumber == 0 ? fullPathOfFiles.insert(i, picFullDir)
                       : fullPathOfFiles.insert(picNumber + i, picFullDir);
        QListWidgetItem *signalPicItem=new QListWidgetItem(QIcon(picFullDir), picFullDir);
        ui->PiclistWidget->addItem(signalPicItem);
    }
    ui->PiclistWidget->setCurrentRow(picNumber);
    picNumber += picFileList.size();
    laodLabelPic(fullPathOfFiles.at(0));
}

void PicOperate::deletePicTriggered()
{
    int curIndex = ui->PiclistWidget->currentRow();
    if (curIndex < 0) { // 图片未导入,用户未点击删除按钮
        fileDirOpen = false;
        return;
    }

    fullPathOfFiles.removeAt(curIndex);
    ui->PiclistWidget->takeItem(curIndex);
    picNumber = fullPathOfFiles.size();
    picImportIndex = picNumber; // 再次打开文件夹或拖拽导入时更新首张位置显示
    if (picNumber > 0) {
        QString str = ui->PiclistWidget->currentItem()->text();
        pinInfoShow(str, ui->PiclistWidget->currentRow(), picNumber);
    } else {
        ui->PicShowLabel->setPixmap(QPixmap(":/new/images/labelBg.jpg"));
        ui->picInfolabel->clear();
        return;
    }
}

// 函数功能:点击QListWidget中不同图片缩略图,Label中显示缩略图对应的大图
void PicOperate::listWidgetRowChanged(int currentRow)
{
    if (currentRow == -1) {
        return;
    }
    QImage currentImg;
    QString currentPic = ui->PiclistWidget->item(currentRow)->text();
    zoomImg.load(currentPic);
    currentImg.load(currentPic);
    ISNULL(currentImg)
    adaptPicLabelShow(currentImg);
    pinInfoShow(currentPic, currentRow, picNumber);
}

// 函数功能:单张图片%10的比例进行放大
void PicOperate::zoomInTriggered()
{
    ISNULL(zoomImg)
    QTransform zoomInTransform;
    zoomInTransform.scale(1.1,1.1);
    zoomImg = zoomImg.transformed(zoomInTransform, Qt::SmoothTransformation);
    ui->PicShowLabel->setPixmap(QPixmap::fromImage(zoomImg));     // Label需设置ScaledContents为true才能随着图片大小自适应
}

// 函数功能:单张图片按原图的%90进行缩小
void PicOperate::zoomOutTriggered()
{
    ISNULL(zoomImg)
    QTransform zoomOutTransform;
    zoomOutTransform.scale(0.9, 0.9);
    zoomImg = zoomImg.transformed(zoomOutTransform, Qt::SmoothTransformation);
    ui->PicShowLabel->setPixmap(QPixmap::fromImage(zoomImg));
}

// 函数功能:跳转至上一张图片
void PicOperate::upPicToolBtnClicked()
{
    if (false == fileDirOpen) {
        tipInformation("请先选择您需要打开的图片文件");
        return;
    }

    picIndex = ui->PiclistWidget->currentRow();
    --picIndex;
    if (picIndex < 0) {
        tipInformation("您当前浏览的是列表中第一张图片,将从列表中最后一张重新开始");
        picIndex = fullPathOfFiles.size() - 1;
    }

    loadSwitchPic(picIndex);
}

// 函数功能:跳转至下一张图片
void PicOperate::downPicToolBtnClicked()
{
    if (false == fileDirOpen) {
        tipInformation("请先选择您需要打开的图片文件");
        return;
    }

    picIndex = ui->PiclistWidget->currentRow();
    picIndex++;
    if (picIndex > fullPathOfFiles.size() - 1) {
        tipInformation("已浏览至最后一张图片,再次点击将从第一张重新开始");
        picIndex = 0;
    }

    loadSwitchPic(picIndex);
}

void PicOperate::leftRotateTriggered()
{
    ISNULL(zoomImg)
    QTransform leftTransform;
    leftTransform.rotate(-rotateAngle);
    zoomImg = zoomImg.transformed(leftTransform, Qt::SmoothTransformation);
    ui->PicShowLabel->setPixmap(QPixmap::fromImage(zoomImg));
}

void PicOperate::rightRotateTriggered()
{
    ISNULL(zoomImg)
    QTransform rightTransform;
    rightTransform.rotate(rotateAngle);
    zoomImg = zoomImg.transformed(rightTransform, Qt::SmoothTransformation);
    ui->PicShowLabel->setPixmap(QPixmap::fromImage(zoomImg));
}


完整版代码及图片资源链接:https://download.csdn.net/download/qq_44896246/87377757

  • 7
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值