1. 程式人生 > 其它 >QT-實現圖片瀏覽器

QT-實現圖片瀏覽器

摘錄自:https://www.cnblogs.com/lifexy/p/9057046.html

painter.cpp

#include "painter.h"

painter::painter()
  :Paint(10 , 10 , 810 , 810)
  ,Alloffset(0 , 0)
  ,label(QString::fromLocal8Bit("100%") , this)
  ,BigButton(QString::fromLocal8Bit("放大") , this)
  ,LittleButton(QString::fromLocal8Bit("縮小") , this)
  ,LiftButton(QString::fromLocal8Bit(
"向左") , this) ,RightButton(QString::fromLocal8Bit("向右") , this) ,UpButton(QString::fromLocal8Bit("向上") , this) ,DownButton(QString::fromLocal8Bit("向下") , this) ,ResetButton(QString::fromLocal8Bit("還原") , this) ,OpenButton(QString::fromLocal8Bit("開啟檔案") , this) { ratio = 1.0; action
= painter::None; BigButton.setGeometry(822 , 10 ,60 ,25); // x y w h connect(&BigButton , SIGNAL(clicked()) , this , SLOT(onBigClicked())); LittleButton.setGeometry(822 , 40 , 60 ,25); connect(&LittleButton , SIGNAL(clicked()) , this , SLOT(onLittleClicked())); LiftButton.setGeometry(
822,70,60,25); connect(&LiftButton,SIGNAL(clicked()),this,SLOT(OnLiftClicked())); RightButton.setGeometry(822,100,60,25); connect(&RightButton,SIGNAL(clicked()),this,SLOT(OnRightClicked())); UpButton.setGeometry(822,130,60,25); connect(&UpButton,SIGNAL(clicked()),this,SLOT(onUpClicked())); DownButton.setGeometry(822,160,60,25); connect(&DownButton,SIGNAL(clicked()),this,SLOT(onDownClicked())); ResetButton.setGeometry(822,190,60,25); connect(&ResetButton,SIGNAL(clicked()),this,SLOT(onResetClicked())); OpenButton.setGeometry(822,220,60,25); connect(&OpenButton,SIGNAL(clicked()),this,SLOT(onOpenClicked())); label.move(840 , 260); resize(890 , 850); this->setWindowTitle(QString::fromLocal8Bit("圖片瀏覽器")); } void painter::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); painter.setRenderHints(QPainter::SmoothPixmapTransform | QPainter::Antialiasing | QPainter::TextAntialiasing); //渲染 //painter.drawRect(Paint.x() - 1 , Paint.y() - 1 , Paint.width() + 1 , Paint.height() + 1); painter.drawRect(Paint.x(), Paint.y(), Paint.width(), Paint.height());//畫框 if(image.isNull()) { return; } int NowW = ratio * pixW; int NowH = ratio * pixH; if(action == painter::Shrink) { ratio -= (0.05 * ratio); if(ratio < 0.018) { ratio = 0.01; } /*顯示比例*/ QString str; str.sprintf("%.0f%",ratio*100); label.setText(str) ; qDebug()<<QString::fromLocal8Bit("縮小:")<<ratio; } else if(action == painter::Amplification) { ratio += 0.05 * ratio; if(ratio>4.5){ ratio = 5.000; } /*顯示比例*/ QString str; str.sprintf("%.0f%",ratio*100); label.setText(str); qDebug()<<QString::fromLocal8Bit("放大:")<<ratio; } if(action == painter::Amplification || action == painter::Shrink || action == painter::Reset) { qDebug() << "111"; NowW = ratio *pixW; NowH = ratio *pixH; crtPix= pix.scaled(NowW, NowH,Qt::KeepAspectRatio,Qt::SmoothTransformation); //重新裝載 action=painter::None; } if(action==painter::Move) //移動 { int offsetx=Alloffset.x()+offset.x(); Alloffset.setX(offsetx); int offsety=Alloffset.y()+offset.y(); Alloffset.setY(offsety); action=painter::None; } if(abs(Alloffset.x())>=(Paint.width()/2 + NowW/2 -10)) //限制X偏移值 { if(Alloffset.x()>0) Alloffset.setX(Paint.width()/2 + NowW/2 -10); else Alloffset.setX(-Paint.width()/2 + -NowW/2 +10); } if(abs(Alloffset.y())>=(Paint.height()/2 + NowH/2 -10)) //限制Y偏移值 { if(Alloffset.y()>0) Alloffset.setY(Paint.height()/2 + NowH/2 -10); else Alloffset.setY(-Paint.height()/2 + -NowH/2 +10); } int x = Paint.width()/2 + Alloffset.x() -NowW/2; if(x<0) x=0; int y = Paint.height()/2 + Alloffset.y() -NowH/2; if(y<0) y=0; int sx = NowW/2 - Paint.width()/2 - Alloffset.x(); if(sx<0) sx=0; int sy = NowH/2 - Paint.height()/2 - Alloffset.y(); if(sy<0) sy=0; int w =(NowW - sx)>Paint.width()? Paint.width() : (NowW - sx); if(w>(Paint.width()-x)) w = Paint.width()-x; int h =(NowH - sy)>Paint.height()? Paint.height() : (NowH - sy); if(h>(Paint.height()-y)) h = Paint.height()-y; painter.drawTiledPixmap(x+Paint.x(),y+Paint.y(),w,h,crtPix,sx,sy); //繪畫圖形 } bool painter::event(QEvent *event) { static bool press = false; static QPoint PreDot; if(event->type() == QEvent::MouseButtonPress) { QMouseEvent *mouse = dynamic_cast<QMouseEvent*>(event); //判斷蘇表左鍵按下且實在繪圖區域內 if(mouse->button() == Qt::LeftButton && Paint.contains(mouse->pos())) { press = true; //QApplication::setOverrideCursor(Qt::OpenHandCursor); // 設定滑鼠樣式 this->setCursor(Qt::OpenHandCursor); PreDot = mouse->pos(); } qDebug() << "event press"; } else if(event->type() == QEvent::MouseButtonRelease) { QMouseEvent *mouse = dynamic_cast<QMouseEvent*>(event); //判斷滑鼠左鍵是否釋放 if(mouse->button() == Qt::LeftButton && press) { QApplication::setOverrideCursor(Qt::ArrowCursor); press = false; } qDebug() << "event relesae"; } if(event->type() == QEvent::MouseMove) { //按下拖動才能實現 if(press) { QMouseEvent *mouse = dynamic_cast<QMouseEvent*>(event); offset.setX(mouse->x() - PreDot.x()); offset.setY(mouse->y() - PreDot.y()); PreDot = mouse->pos(); action = painter::Move; this->update(); } qDebug() << "event move"; } return QWidget::event(event); } void painter::wheelEvent(QWheelEvent* event) //滑鼠滑輪事件 { if(event->delta()>0) { //上滑,縮小 action=painter::Shrink; this->update(); } else { //下滑,放大 action=painter::Amplification; this->update(); } event->accept(); } void painter::onLittleClicked() { action=painter::Amplification; this->update(); } void painter::onOpenClicked() { QString str = QFileDialog::getOpenFileName(this, "open", "E:", "img (*.png *.jpg)"); if(!str.isNull()) { image.load(str); pix = pix.fromImage(image); crtPix = pix; pixW = image.width(); //圖片寬 pixH = image.height(); //圖片高 qDebug()<<str<<pixW<<pixH; this->setWindowTitle(QStringLiteral("image read") + "(" + str + ")"); onResetClicked(); } } void painter::onBigClicked() { action=painter::Shrink; this->update(); } void painter::onUpClicked() { action=painter::Move; offset.setX(0); offset.setY(-20); this->update(); } void painter::onDownClicked() { action=painter::Move; offset.setX(0); offset.setY(20); this->update(); } void painter::onResetClicked() { action=painter::Reset; Alloffset.setX(0); Alloffset.setY(0); ratio = 1.000; label.setText("100%"); this->update(); } void painter::OnLiftClicked() { action=painter::Move; offset.setX(-20); offset.setY(0); this->update(); } void painter::OnRightClicked() { action=painter::Move; offset.setX(20) ; offset.setY(0) ; this->update(); }

painter.h

#ifndef PAINTER_H
#define PAINTER_H

#include <QWidget>
#include <QtGui>
#include <QLabel>
#include <QPushButton>
#include <QComboBox>
#include <QApplication>
#include <QFileDialog>
#include <QImage>

class painter : public QWidget
{
    Q_OBJECT
private :
    QPixmap  pix;
    QPixmap  crtPix;
    int action;          //動作(放大,縮小,移動...)
    int pixW;            //圖片寬
    int pixH;            //圖片高

    QRect Paint;         //繪畫區域

    QImage image;        //開啟的圖片


    float ratio;              //縮放比例
    QPoint offset;           //一次的圖片偏移值
    QPoint Alloffset;          //總偏移
    QLabel label;

    QPushButton  BigButton;
    QPushButton  LittleButton;
    QPushButton  LiftButton;
    QPushButton  RightButton;
    QPushButton  UpButton;
    QPushButton  DownButton;
    QPushButton  ResetButton;
    QPushButton  OpenButton;

    void AddComboItem(QComboBox* cmbo);
    bool event(QEvent * event);
    void wheelEvent(QWheelEvent* e);     //滑鼠滑輪事件
private slots:
    void onUpClicked();
    void onDownClicked();
    void onResetClicked();
    void OnLiftClicked();
    void OnRightClicked();
    void onLittleClicked();
    void onBigClicked();

    void onOpenClicked();

    void paintEvent(QPaintEvent *event);

public:
    explicit painter();

   enum  Type {
       None          = 0,
       Amplification ,
       Shrink,
       Lift,
       Right,
       Up,
       Down,
       Move,
       Reset
   };

signals:

};

#endif // PAINTER_H

主要通過滑鼠事件與定義的事件相關聯,並通過重寫painterEvent實現相關操作。實現縮小放大,移動的邏輯跟我上篇相似。