创建一个场景作为显示图像的画框,在Qt里使用QGraphicsView、QGraphicsScene等进行绘制场景显示图像。
创建一个类PixItem继承于QGraphicsItem然后实现重绘、滚轮、鼠标拖放等功能,主要用于加载解析好的heic格式图像,然后对图像进行一些操作,比如放大、缩小、鼠标拖放等功能。下面是具体实现的类。
- class PixItem : public QGraphicsItem //继承自图元类
- {
- public:
- PixItem(QPixmap pixmap); //构造函数初始化了变量pix
- QRectF boundingRect() const; //实现自己的boundingRect
- void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); //重绘
- void wheelEvent(QGraphicsSceneWheelEvent *event);
- void setZoomState(const int &zoomState);
-
- void mousePressEvent(QGraphicsSceneMouseEvent *event);
- void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
- void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
-
- int getScaleValue() const;
- void setScaleValue(const int &);
-
- private:
- qreal m_scaleValue; //缩放值
- QPixmap pix; //作为图元显示的图片
- int m_zoomState;
-
- bool m_isMove;
- QPointF m_startPos;
-
- FileDialogEx *m_fileDialogEx;
- };
通过重绘图像,重写鼠标点击事件、移动事件、释放等事件,来对图像进行控制,需要注意的是需要在构造函数中加入:setAcceptDrops(true);实现图像拖拽的前提。
然后对每个函数进行实现:
(1)实现图像的边界函数
- QRectF PixItem::boundingRect() const
- {
- return QRectF(-pix.width() / 2, -pix.height() / 2,
- pix.width(), pix.height());
- }
(2)将图元图片绘出
- void PixItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
- QWidget *)
- {
- painter->drawPixmap(-pix.width() / 2, -pix.height() / 2, pix);
- }
这样的话,图像就可以显示出来了,主要是需要在paint函数中实现drawPixmap函数。但是想要对图像进一步操作,还需要继续实现头文件中定义的其他函数,我们一个一个来看。
(3)对图像进行局部缩放
图像的局部缩放主要用到了setScale函数,它可以控制图像的大小:
- //鼠标点击事件 局部缩放
- void PixItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
- {
- m_startPos = event->pos();
- m_isMove = true;
-
- int scaleValue = m_scaleValue;
- if(m_zoomState == ZOOM_IN) //局部放大
- {
- scaleValue++;
- }
- else if(m_zoomState == ZOOM_OUT) //局部缩小
- {
- scaleValue--;
- }
-
- if (scaleValue > ZOOM_IN_TIMES || scaleValue < ZOOM_OUT_TIMES)
- return;
-
- if (m_scaleValue != scaleValue)
- {
- setTransformOriginPoint(event->pos().x(), event->pos().y());
- }
- m_scaleValue = scaleValue;
- qreal s;
- //实现局部缩放
- if(m_scaleValue > 0)
- {
- s = pow(1.1, m_scaleValue); //放大 计算x的y方次 参数都是double类型
- }else
- {
- s = pow(1 / 1.1, -m_scaleValue); //缩小
- }
- setScale(s);
- }
(4)鼠标的移动事件
- void PixItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
- {
- if(m_isMove)
- {
- QPointF point = event->pos() - m_startPos;
- moveBy(point.x(), point.y());
-
- }
-
- }
-
- void PixItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
- {
- m_isMove = false;
- }
上述画布类实现完成后,就可以作为显示图像的画布了。主要实现显示图像、当滑动鼠标是图像放大缩小,然后通过鼠标左键进行拖拽实现图像的移动。