本例程通过重写了一个类,继承自QGraphicItem,来实现了在qgraphicsScene上绘制、拖动、缩放、旋转矩形。
效果如下:

其实要实现绘制、拖动、缩放矩形都不难,难的是在旋转之后还要支持缩放。
我的思路是:
1.实现绘制矩形:只要定义一个全局变量QRectF m_oldRect,在外面矩形大小传进来,然后在paint函数里面绘制这个矩形就行
2.实现拖动矩形:重写mousePressEvent,mouseMoveEvent,mouseReleaseEvent,记录鼠标按下的起始点和移动时候的点,并用moveBy()函数来移动矩形即可
3.实现缩放:在矩形内部靠近4条边的地方定义4个矩形,当鼠标按下的时候在这4个矩形方框内,则将矩形往4个方向拉伸
4.实现旋转:
我给之前定义的矩形全部配一个QPolygonF,因为我只能通过绘制多边形的方式来画出旋转之后的矩形。
矩形正上方的圆圈我是通过三角函数+直线方程来计算,让其始终绘制在矩形左右两个顶点的中垂线上方。
当鼠标落在圆形内部的时候可以控制矩形旋转。
在矩形旋转之后,再进行拉伸的时候,我是通过的计算鼠标的点离对面的边的距离来重新计算矩形的大小,然后计算对应的QPolygonF的大小。
下载连接如下:
https://download.csdn.net/download/weixin_43935474/75230501
主要代码如下:
mygraphicrectitem.h
#ifndef MYGRAPHICRECTITEM_H
#define MYGRAPHICRECTITEM_H
#include <QObject>
#include <QWidget>
#include <QMouseEvent>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QGraphicsSceneMouseEvent>
#include <QRect>
#include <QPainter>
#include <QPolygon>
#include <QList>
enum STATE_FLAG{
DEFAULT_FLAG=0,
MOV_LEFT_LINE,//标记当前为用户按下矩形的左边界区域
MOV_TOP_LINE,//标记当前为用户按下矩形的上边界区域
MOV_RIGHT_LINE,//标记当前为用户按下矩形的右边界区域
MOV_BOTTOM_LINE,//标记当前为用户按下矩形的下边界区域
MOV_RIGHTBOTTOM_RECT,//标记当前为用户按下矩形的右下角
MOV_RECT,//标记当前为鼠标拖动图片移动状态
ROTATE//标记当前为旋转状态
};
class myGraphicRectItem:public QObject,public QGraphicsItem
{
Q_OBJECT
public:
myGraphicRectItem(QGraphicsItem *parent = nullptr);
//myGraphicRectItem(QRectF m_OriginRect = QRectF(0,0,100,100));
QRectF boundingRect() const;
~myGraphicRectItem();
void setRectSize(QRectF mrect,bool bResetRotateCenter = true);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void SetRotate(qreal RotateAngle,QPointF ptCenter=QPointF(-999,-999));
QPointF getRotatePoint(QPointF ptCenter, QPointF ptIn, qreal angle);//获取旋转后的点
QList<QPointF> getRotatePoints(QPointF ptCenter,QList<QPointF> ptIns,qreal angle);//获取多个旋转后的点
QPolygonF getRotatePolygonFromRect(QPointF ptCenter,QRectF rectIn,qreal angle);//将矩形旋转之后返回多边形
QRectF getCrtPosRectToSceen();
QRectF m_SmallRotateRect;//矩形顶部用来表示旋转的标记的矩形
QPolygonF m_SmallRotatePolygon;//矩形顶部用来表示旋转的标记的矩形旋转后形成的多边形
QPointF getSmallRotateRectCenter(QPointF ptA,QPointF ptB);//获取旋转时候矩形正上方的旋转标记矩形
QRectF getSmallRotateRect(QPointF ptA,QPointF ptB);
bool m_bRotate;
qreal m_RotateAngle;
QPointF m_RotateCenter;
private:
QRectF m_oldRect;
QPolygonF m_oldRectPolygon;
QRectF m_RotateAreaRect;
bool m_bResize;
QPolygonF m_insicedPolygon;
QRectF m_insicedRectf;
QPolygonF m_leftPolygon;
QRectF m_leftRectf;
QPolygonF m_topPolygon;
QRectF m_topRectf;
QPolygonF m_rightPolygon;
QRectF m_rightRectf;
QPolygonF m_bottomPolygon;
QRectF m_bottomRectf;
// QPolygonF m_rbPolygon;
// QRectF m_rbRectf;
QPointF m_startPos;
STATE_FLAG m_StateFlag;
QPointF *pPointFofSmallRotateRect;
protected:
};
#endif // MYGRAPHICRECTITEM_H
mygraphicrectitem.cpp
#include "mygraphicrectitem.h"
#include <QtMath>
#include <QDebug>
myGraphicRectItem::myGraphicRectItem(QGraphicsItem *parent):
m_bResize(false),
m_oldRect(0,0,100,100),
m_bRotate(false),
m_RotateAngle(0),
m_StateFlag(DEFAULT_FLAG)
{
//setParent(parent);
setRectSize(m_oldRect);
setToolTip("Click and drag me!"); //提示
setCursor(Qt::ArrowCursor); //改变光标形状,手的形状
setFlag(QGraphicsItem::ItemIsMovable);
// setAcceptDrops(true);
pPointFofSmallRotateRect = new QPointF[4];
SetRotate(0);
setFlag(QGraphicsItem::ItemIsSelectable);//
}
QRectF myGraphicRectItem::boundingRect() const
{
//return m_oldRectPolygon.boundingRect();
QRectF boundingRectF = m_oldRectPolygon.boundingRect();
return QRectF(boundingRectF.x()-40,boundingRectF.y()-40,boundingRectF.width()+80,boundingRectF.height()+80);
}
myGraphicRectItem::~myGraphicRectItem()
{
delete []pPointFofSmallRotateRect;
pPointFofSmallRotateRect = nullptr;
}
void myGraphicRectItem::setRectSize(QRectF mrect, bool bResetRotateCenter)
{
m_oldRect = mrect;
if(bResetRotateCenter)
{
m_RotateCenter.setX(m_oldRect.x()+m_oldRect.width()/2);
m_RotateCenter.setY(m_oldRect.y()+m_oldRect.height()/2);
}
m_oldRectPolygon = getRotatePolygonFromRect(m_RotateCenter,m_oldRect,m_RotateAngle);
m_insicedRectf = QRectF(m_oldRect.x()+8,m_oldRect.y()+8,m_oldRect.width()-16,m_oldRect.height()-16);
m_insicedPolygon =getRotatePolygonFromRect(m_RotateCenter,m_insicedRectf,m_RotateAngle);
m_leftRectf = QRectF(m_oldRect.x(),m_oldRect.y(),8,m_oldRect.height()-8);
m_leftPolygon = getRotatePolygonFromRect(m_RotateCenter,m_leftRectf,m_RotateAngle);
m_topRectf = QRectF(m_oldRect.x()+8,m_oldRect.y(),m_oldRect.width()-8,8);
m_topPolygon = getRotatePolygonFromRect(m_RotateCenter,m_topRectf,m_RotateAngle);
m_rightRectf = QRectF(m_oldRect.right()-8,m_oldRect.y()+8,8,m_oldRect.height()-16);
m_rightPolygon = getRotatePolygonFromRect(m_RotateCenter,m_rightRectf,m_RotateAngle);
m_bottomRectf = QRectF(m_oldRect.x(),m_oldRect.bottom()-8,m_oldRect.width()-8,8);
m_bottomPolygon = getRotatePolygonFromRect(m_RotateCenter,m_bottomRectf,m_RotateAngle);
// m_rbRectf = QRectF(m_oldRect.right()-8,m_oldRect.bottom()-8,8,8);
// m_rbPolygon = getRotatePolygonFromRect(m_RotateCenter,m_rbRectf,m_RotateAngle);
m_SmallRotateRect = getSmallRotateRect(mrect.topLeft(),mrect.topRight(

本文介绍了一种在Qt的QGraphicsScene中实现矩形的绘制、拖动、缩放和旋转的方法。通过重写QGraphicsItem类,文章详细解释了如何在矩形旋转后仍支持缩放操作,包括利用QPolygonF进行旋转处理和通过计算实现精确的缩放控制。

5496

被折叠的 条评论
为什么被折叠?



