首页 > 代码库 > Qt 图片浏览器 实现图片的放大缩小翻转等功能

Qt 图片浏览器 实现图片的放大缩小翻转等功能

图片的功能


源代码:

wiget.h

#ifndef WIDGET_H
#define WIDGET_H
#include <QPixmap>
#include <QImage>
#include <QPushButton>
#include <QHBoxLayout>
#include <QScrollArea>
#include <QLabel>
#include <QWidget>
#include <QPalette>
#include <QTimer>
#include <QString>
#include <QPainter>
#include <QPaintEvent>
#include <QDebug>
#include <QFileDialog>
#include <QStringList>
#include <QMessageBox>
#include <QDir>
#include <QFile>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
    Q_OBJECT  
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
    QLabel *label;
    QTimer *timer;
    QPixmap pix;
private slots:
    void W_timergo();
    void W_go();
    void W_back();
    void W_close();
    void W_start();
    void W_stop();
    void W_max();
    void W_min();
    void W_flip();
    void W_delete();
    void W_open();
private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

wiget.cpp

#include "widget.h"
#include "ui_widget.h"
static int num;
static int flag=1;
QString image[3] = {":/prefix1/1.jpg",":/prefix1/2.jpg",":/prefix1/3.jpg"};
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    QPixmap pix;
    label = new QLabel;
    timer = new QTimer;
    QPalette palette;
    ui->scrollArea->setWidget(label);
    ui->scrollArea->setAlignment(Qt::AlignCenter);
    palette.setBrush(QPalette::Background,QBrush(QPixmap(":/prefix1/4.jpg")));
    this->setPalette(palette);
    connect(timer,SIGNAL(timeout()),this,SLOT(W_go()));
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(W_go()));
    connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(W_close()));
    connect(ui->pushButton_4,SIGNAL(clicked()),this,SLOT(W_start()));
    connect(ui->pushButton_5,SIGNAL(clicked()),this,SLOT(W_stop()));
    connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(W_back()));
    connect(ui->pushButton_6,SIGNAL(clicked()),this,SLOT(W_max()));
    connect(ui->pushButton_7,SIGNAL(clicked()),this,SLOT(W_min()));
    connect(ui->pushButton_8,SIGNAL(clicked()),this,SLOT(W_flip()));
    connect(ui->pushButton_9,SIGNAL(clicked()),this,SLOT(W_delete()));
    connect(ui->pushButton_10,SIGNAL(clicked()),this,SLOT(W_open()));
}
void Widget::W_timergo()
{
   pix.load(image[num]);
   label->setPixmap(pix);
   if(num==2)
   {
        num = -1;
   }
   num++;
}
void Widget::W_go()
{
    timer->stop();
    pix.load(image[num]);
    label->setPixmap(pix);
    if(num==2)
    {
         num = -1;
    }
    num++;
}
void Widget::W_back()
{
    if(num < 0)
    {
        num = 2;
    }
    timer->stop();
    pix.load(image[num]);
    label->setPixmap(pix);
    num--;
}
void Widget::W_close()
{
    this->close();
}
void Widget::W_start()
{
     timer->start(2000);
}
void Widget::W_stop()
{
    timer->stop();
}
void Widget::W_max()
{
    timer->stop();
    float MaxX,MaxY;
    MaxX = pix.height();
    MaxY = pix.width();
    pix = pix.scaled(MaxX*2,MaxY*2,Qt::KeepAspectRatio);
    label->setPixmap(pix);
}
void Widget::W_min()
{
    timer->stop();
    float MaxX,MaxY;
    MaxX = pix.height();
    MaxY = pix.width();
    pix = pix.scaled(MaxX/2,MaxY/2,Qt::KeepAspectRatio);
    label->setPixmap(pix);
}
void Widget::W_flip()
{
    timer->stop();
    QMatrix leftmatrix;
    leftmatrix.rotate(90*flag);
    flag++;
    if(flag == 4)
    {
        flag = 0;
    }
    label->setPixmap(QPixmap(image[num]).transformed(leftmatrix,Qt::SmoothTransformation));
}
void Widget::W_delete()
{
    timer->stop();
    QStringList list;
    list=QFileDialog::getOpenFileNames(this,"open image","/","*.jpg");
    qDebug()<<list.at(0);
    pix.load(list.at(0));
    label->setPixmap(pix);
    QMessageBox::StandardButton rb = QMessageBox::question(NULL,"Warning",
                    "Do you want to delete the picture?",QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
       if(rb == QMessageBox::Yes)
       {
           QFile::remove(list.at(0));//刪除文件
       }
}
void Widget::W_open()
{
    timer->stop();
    QStringList list;
    list=QFileDialog::getOpenFileNames(this,"open image","/","*.jpg");
     qDebug()<<list.at(0);
    pix.load(list.at(0));
    label->setPixmap(pix);
}
Widget::~Widget()
{
    delete ui;
}
main.cpp

#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}





Qt 图片浏览器 实现图片的放大缩小翻转等功能