700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > QT下实现图片的自适应显示 缩放(以鼠标位置为中心进行缩放) 拖动 以及重置

QT下实现图片的自适应显示 缩放(以鼠标位置为中心进行缩放) 拖动 以及重置

时间:2020-03-04 11:59:58

相关推荐

QT下实现图片的自适应显示 缩放(以鼠标位置为中心进行缩放) 拖动 以及重置

系统配置:ThinkPad T570、Windows10、QT5.12.2(Qt Creater 4.8.2)

我是通过qt中的Graphics View控件来实现上述功能的。

我的源码下载链接:/download/weixin_43935474/68826322

代码实现具体步骤如下:

1.新建一个qt界面工程,将一个Graphics View控件拖动到主界面,如下图:

2.在mainwindow.h中添加如下代码:

在文件头包含自己写的类

#include "imagewidget.h"

在类中添加:

private:ImageWidget*m_Image;public:void recvShowPicSignal(QImage image);//接收并显示图片的函数

3.在mainwindow.cpp中添加recvShowPicSignal函数实体,如下:

void MainWindow::recvShowPicSignal(QImage image){QPixmap ConvertPixmap=QPixmap::fromImage(image);//The QPixmap class is an off-screen image representation that can be used as a paint deviceQGraphicsScene *qgraphicsScene = new QGraphicsScene;//要用QGraphicsView就必须要有QGraphicsScene搭配着用m_Image = new ImageWidget(&ConvertPixmap);//实例化类ImageWidget的对象m_Image,该类继承自QGraphicsItem,是自己写的类int nwith = ui->ImageGraphic->width();//获取界面控件Graphics View的宽度int nheight = ui->ImageGraphic->height();//获取界面控件Graphics View的高度m_Image->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控件}

4.新建一个c++类ImageWidget,头文件 imagewidget.h内容如下:

#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, QGraphicsItemclass ImageWidget :public QGraphicsItem{//Q_OBJECTpublic: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);private:qreal m_scaleValue;qreal m_scaleDafault;QPixmapm_pix;int m_zoomState;bool m_isMove;QPointFm_startPos;};#endif // IMAGEWIDGET_H

5.ImageWidget的源文件imagewidget.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();//鼠标左击时,获取当前鼠标在图片中的坐标,m_isMove = true;//标记鼠标左键被按下}else if(event->button() == Qt::RightButton){ResetItemPos();//右击鼠标重置大小}}void ImageWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event){if(m_isMove){QPointF point = (event->pos() - m_startPos)*m_scaleValue;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;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;}

6.导入图片的功能在此就不赘述了,最终显示效果如下:

欢迎各位加我微信进行技术交流:13382893223

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。