点击上方“Qt学视觉”,选择“星标”公众号重磅干货,第一时间送达
共同学习共同进步
图片显示控件采用graphicsView组件,也就是QGraphicsView类
写一个类来继承QGraphicsItem,来控制图片的平移放大缩小等操作
#pragma once#include #include #include #include #include #include #include #include #include #include enum Enum_ZoomState { NO_STATE, RESET, ZOOM_IN, ZOOM_OUT};class QImageWidget : public QGraphicsItem{public: QImageWidget(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; QPixmap m_pix; int m_zoomState; bool m_isMove; QPointF m_startPos;};#include "QImageWidget.h"#include #include #include #include #include #include QImageWidget::QImageWidget(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 QImageWidget::boundingRect() const{ return QRectF(-m_pix.width() / 2, -m_pix.height() / 2, m_pix.width(), m_pix.height());}void QImageWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*){ painter->drawPixmap(-m_pix.width() / 2, -m_pix.height() / 2, m_pix);}void QImageWidget::mousePressEvent(QGraphicsSceneMouseEvent* event){ if (event->button() == Qt::LeftButton) { m_startPos = event->pos();//鼠标左击时,获取当前鼠标在图片中的坐标, m_isMove = true;//标记鼠标左键被按下 } else if (event->button() == Qt::RightButton) { ResetItemPos();//右击鼠标重置大小 }}void QImageWidget::mouseMoveEvent(QGraphicsSceneMouseEvent* event){ if (m_isMove) { QPointF point = (event->pos() - m_startPos) * m_scaleValue; moveBy(point.x(), point.y()); }}void QImageWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent*){ m_isMove = false;//标记鼠标左键已经抬起}void QImageWidget::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);//使图片缩放的效果看起来像是以鼠标所在点为中心进行缩放的 } }}//将主界面的控件QGraphicsView的width和height传进本类中,并根据图像的长宽和控件的长宽的比例来使图片缩放到适合控件的大小void QImageWidget::setQGraphicsViewWH(int nwidth, int nheight){ 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 QImageWidget::ResetItemPos(){ m_scaleValue = m_scaleDafault;//缩放比例回到一开始的自适应比例 setScale(m_scaleDafault);//缩放到一开始的自适应大小 setPos(0, 0);}qreal QImageWidget::getScaleValue() const{ return m_scaleValue;}
再主窗口进行打开图片显示
打开图片操作如下
//打开图片文件
void OpenCVTestSf::OpenImageFile(){ QString fileName = QFileDialog::getOpenFileName(this, "open Image", "", "Image File(*.bmp *.jpg *.jpeg *.png)"); QTextCodec* code = QTextCodec::codecForName("gb18030"); std::string name = code->fromUnicode(fileName).data(); m_srcImage = imread(name); if (m_srcImage.data) { cvtColor(m_srcImage, m_srcImage, COLOR_BGR2RGB);//BGR转化为RGB QImage::Format format = QImage::Format_RGB888; switch (m_srcImage.type()) { case CV_8UC1: format = QImage::Format_Indexed8; break; case CV_8UC3: format = QImage::Format_RGB888; break; case CV_8UC4: format = QImage::Format_ARGB32; break; } QImage img = QImage((const uchar*)m_srcImage.data, m_srcImage.cols, m_srcImage.rows, m_srcImage.cols * m_srcImage.channels(), format); recvShowPicSignal(img); }}
显示图片如下
//显示图片
void OpenCVTestSf::recvShowPicSignal(QImage image){ 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搭配着用 m_pImage = new QImageWidget(&ConvertPixmap);//实例化类ImageWidget的对象m_Image,该类继承自QGraphicsItem,是自己写的类 int nwith = ui.graphicsViewImgShow->width();//获取界面控件Graphics View的宽度 int nheight = ui.graphicsViewImgShow->height();//获取界面控件Graphics View的高度 m_pImage->setQGraphicsViewWH(nwith, nheight);//将界面控件Graphics View的width和height传进类m_Image中 qgraphicsScene->addItem(m_pImage);//将QGraphicsItem类对象放进QGraphicsScene中 ui.graphicsViewImgShow->setSceneRect(QRectF(-(nwith / 2), -(nheight / 2), nwith, nheight));//使视窗的大小固定在原始大小,不会随图片的放大而放大(默认状态下图片放大的时候视窗两边会自动出现滚动条,并且视窗内的视野会变大),防止图片放大后重新缩小的时候视窗太大而不方便观察图片 ui.graphicsViewImgShow->setScene(qgraphicsScene);//Sets the current scene to scene. If scene is already being viewed, this function does nothing. ui.graphicsViewImgShow->setFocus();//将界面的焦点设置到当前Graphics View控件}
效果如下