1. 程式人生 > 其它 >QT圖片縮放、拖動、重置

QT圖片縮放、拖動、重置

技術標籤:QT5.7QT圖片放大縮小

  • 效果圖

  • 程式碼思路

通過繼承QGraphicsView自定義檢視,重寫wheelevent、mousePressEvent、mouseMoveEvent等事件,實現影象的自適應大小、滾輪縮放、滑鼠拖動、圖片重置等功能

  • 檔案頭

#ifndef IMAGEWIDGET_H
#define IMAGEWIDGET_H

#include <QWidget>
#include <QtGui>
#include <QPixmap>
#include <QPainter>
#include <QRectF>
#include <QMouseEvent>
#include <QPointF>
#include <QDragEnterEvent>
#include <QGraphicsSceneWheelEvent>
#include <QGraphicsItem>

enum Enum_ZoomState{
    NO_STATE,
    RESET,
    ZOOM_IN,
    ZOOM_OUT
};
// class ImageWidget :public QObject, QGraphicsItem
class ImageWidget :public QGraphicsItem
{
    //Q_OBJECT
public:
    ImageWidget(QPixmap *pixmap);
    QRectF  boundingRect() const;
    void    paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    void    wheelEvent(QGraphicsSceneWheelEvent *event);
    void    ResetItemPos();
    void    mousePressEvent(QGraphicsSceneMouseEvent *event);
    void    mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    void    mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    qreal   getScaleValue() const;
    void    setQGraphicsViewWH(int nwidth,int nheight);

    void    setRectPos(QRect *rect1,QRect *rect2);
//private:
    qreal       m_scaleValue;
    qreal       m_scaleDafault;
    QPixmap     m_pix;
    int         m_zoomState;
    bool        m_isMove;
    QPointF     m_startPos;
};
#endif // IMAGEWIDGET_H
  • cpp檔案
#include "imagewidget.h"
#include <QDebug>
#include <QGraphicsSceneMouseEvent>
#include <QPointF>
#include <QGraphicsSceneDragDropEvent>
#include <QDrag>
#include <math.h>

ImageWidget::ImageWidget(QPixmap *pixmap)
{
    m_pix = *pixmap;
    setAcceptDrops(true);//If enabled is true, this item will accept hover events; otherwise, it will ignore them. By default, items do not accept hover events.
    m_scaleValue = 0;
    m_scaleDafault = 0;
    m_isMove = false;
}

QRectF ImageWidget::boundingRect() const
{
    return QRectF(-m_pix.width() / 2, -m_pix.height() / 2,
                  m_pix.width(), m_pix.height());
}

void ImageWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
                    QWidget *)
{
    painter->drawPixmap(-m_pix.width() / 2, -m_pix.height() / 2, m_pix);

}

void ImageWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if(event->button()== Qt::LeftButton)
    {
        m_startPos = event->pos();//滑鼠左擊時,獲取當前滑鼠在圖片中的座標,
        //qDebug()<<m_startPos;
        m_isMove = true;//標記滑鼠左鍵被按下
    }
    else if(event->button() == Qt::RightButton)
    {
        ResetItemPos();//右擊滑鼠重置大小
    }

}

void ImageWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if(m_isMove)
    {

        //qDebug()<<"mouseMoveEvent"<<event->pos()<<m_startPos<<m_scaleValue;
        QPointF point = (event->pos() - m_startPos)*m_scaleValue;
        //qDebug()<<"point"<<point;
        moveBy(point.x(), point.y());
    }
}

void ImageWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
{
    m_isMove = false;//標記滑鼠左鍵已經抬起
}


void ImageWidget::wheelEvent(QGraphicsSceneWheelEvent *event)//滑鼠滾輪事件
{
    if((event->delta() > 0)&&(m_scaleValue >= 50))//最大放大到原始影象的50倍
    {
        return;
    }
    else if((event->delta() < 0)&&(m_scaleValue <= m_scaleDafault))//影象縮小到自適應大小之後就不繼續縮小
    {
        ResetItemPos();//重置圖片大小和位置,使之自適應控制元件視窗大小
    }
    else
    {
        qreal qrealOriginScale = m_scaleValue;
        if(event->delta() > 0)//滑鼠滾輪向前滾動
        {
            m_scaleValue*=1.1;//每次放大10%
        }
        else
        {
            m_scaleValue*=0.9;//每次縮小10%
        }
        setScale(m_scaleValue);
        if(event->delta() > 0)
        {
            moveBy(-event->pos().x()*qrealOriginScale*0.1, -event->pos().y()*qrealOriginScale*0.1);//使圖片縮放的效果看起來像是以滑鼠所在點為中心進行縮放的
        }
        else
        {
            moveBy(event->pos().x()*qrealOriginScale*0.1, event->pos().y()*qrealOriginScale*0.1);//使圖片縮放的效果看起來像是以滑鼠所在點為中心進行縮放的
        }
    }
}

void ImageWidget::setQGraphicsViewWH(int nwidth, int nheight)//將主介面的控制元件QGraphicsView的width和height傳進本類中,並根據影象的長寬和控制元件的長寬的比例來使圖片縮放到適合控制元件的大小
{
    int nImgWidth = m_pix.width();
    int nImgHeight = m_pix.height();
    qreal temp1 = nwidth*1.0/nImgWidth;
    qreal temp2 = nheight*1.0/nImgHeight;
    //qDebug()<<nImgWidth<<nImgHeight<<temp1<<temp2<<endl;
    if(temp1>temp2)
    {
        m_scaleDafault = temp2;
    }
    else
    {
        m_scaleDafault = temp1;
    }
    setScale(m_scaleDafault);
    m_scaleValue = m_scaleDafault;
}

void ImageWidget::ResetItemPos()//重置圖片位置
{
    m_scaleValue = m_scaleDafault;//縮放比例回到一開始的自適應比例
    setScale(m_scaleDafault);//縮放到一開始的自適應大小
    setPos(0,0);
}

qreal ImageWidget::getScaleValue() const
{
    return m_scaleValue;
}
void ImageWidget::setRectPos(QRect *rect1, QRect *rect2)
{
   // 根據矩形位置進行聯動
    //固定縮放比例
    //根據傳入座標計算縮放後位置

    QPointF point(rect1->left()-m_pix.width()/2,rect1->top()-m_pix.height()/2);

    //moveBy(point.x()/10, point.y()/10);

    QGraphicsSceneMouseEvent *event = new QGraphicsSceneMouseEvent();
    event->setPos(point);
    qDebug()<<"setRectPos"<<event->pos();
    m_isMove = true;
    int k = 1;
    if(point.y()>0)
        k = 1;
    else
        k = -1;
    m_startPos = QPointF(event->pos().x(),(m_pix.height()/2)*k);
    this->mouseMoveEvent(event);

}
  • 使用,ui介面將Graphics View控制元件拖動到主介面
QImage image;
image.load(imgUrl);

QPixmap ConvertPixmap=QPixmap::fromImage(image);//The QPixmap class is an off-screen image representation that can be used as a paint device
QGraphicsScene  *qgraphicsScene = new QGraphicsScene;//要用QGraphicsView就必須要有QGraphicsScene搭配著用
ImageWidget  *imageWidget= new ImageWidget(&ConvertPixmap);//例項化類
int nwith = ui->ImageGraphic->width();//獲取介面控制元件Graphics View的寬度
int nheight = ui->ImageGraphic->height();//獲取介面控制元件Graphics View的高度
imageWidget->setQGraphicsViewWH(nwith,nheight);//將介面控制元件Graphics View的width和height傳進類m_Image中
qgraphicsScene->addItem(m_Image);//將QGraphicsItem類物件放進QGraphicsScene中
ui->ImageGraphic->setSceneRect(QRectF(-(nwith/2),-(nheight/2),nwith,nheight));//使視窗的大小固定在原始大小,不會隨圖片的放大而放大(預設狀態下圖片放大的時候視窗兩邊會自動出現滾動條,並且視窗內的視野會變大),防止圖片放大後重新縮小的時候視窗太大而不方便觀察圖片
ui->ImageGraphic->setScene(qgraphicsScene);//Sets the current scene to scene. If scene is already being viewed, this function does nothing.
ui->ImageGraphic->setFocus();//將介面的焦點設定到當前Graphics View控制元件