vs2022+qt6.6.3重写QGraphicsItem绘制一个矩形。该矩形包括5个锚点Item,能够实现矩形的形状调整和移动。这里分享自己的实现思路,难免会有错误,欢迎批评指正。
1.思路
1.1 依赖关系
实现拖动锚点,矩形自动重绘和移动,锚点图元、自定义矩形图元和场景之间的关系非常重要。
(1)锚点图元是自定义矩形图元的子Item。当移动锚点图元时,会将鼠标在场景的新位置传递给父item,然后根据锚点类型进行重绘或者移动自定义矩形。
(2)自定义矩形图元是核心,是绘制的主要部分,主要负责在初始化阶段在四个边角和中心绘制锚点,以及接收锚点图元的新坐标位置重绘矩形。
(3)场景相当于画布,是自定义图元和锚点图元绘制的地方,坐标一般都是基于场景。
1.2 逻辑关系
自定义图元初始化锚点需要#include锚点文件
移动锚点图元时会将坐标传递给自定义图元,需要#include自定义图元文件
两个类头文件互相引用会报错
因此对自定义图元抽象出一个类ItemBase,然后让自定义图元继承这个类,这样就不会互相引用。在h文件中创建一个传递新锚点坐标的纯虚函数。然后自定义图元去继承ItemBase类并重写updateNewPosition函数,这样锚点图元传递坐标就会传递给自定义图元。
#pragma once
#include"qgraphicsitem.h"
#include"qpainter"
class ItemBase:public QAbstractGraphicsShapeItem
{
public:
enum AnchorType
{
LU,
LD,
RU,
RD,
CENTER,
ROTATE
};
public:
int m_itemType;
virtual void updateNewPosition(QPointF p, AnchorType type) = 0;
};
2.自定义矩形图元实现
2.1 构造函数
在自定义图元中初始化5个锚点,设置锚点的位置、类型以及父图元。
RectItem::RectItem(QRectF rect)
{
//初始化矩形位置
m_rect = rect;
m_x = rect.x();
m_y = rect.y();
m_width = rect.width();
m_height = rect.height();
m_center = rect.center();
m_itemType = Rect;
//定义锚点
m_anchorPointList.append(new AnchorPoint(LU));
m_anchorPointList.append(new AnchorPoint(LD));
m_anchorPointList.append(new AnchorPoint(RU));
m_anchorPointList.append(new AnchorPoint(RD));
m_anchorPointList.append(new AnchorPoint(CENTER));
//初始化锚点位置
m_anchorPointList[0]->setPoint(QPointF(m_x, m_y));
m_anchorPointList[1]->setPoint(QPointF(m_x, m_y + m_height));
m_anchorPointList[2]->setPoint(QPointF(m_x + m_width, m_y));
m_anchorPointList[3]->setPoint(QPointF(m_x + m_width, m_y + m_height));
m_anchorPointList[4]->setPoint(m_center);
//设置父图元
m_anchorPointList[0]->setParentItem(this);
m_anchorPointList[1]->setParentItem(this);
m_anchorPointList[2]->setParentItem(this);
m_anchorPointList[3]->setParentItem(this);
m_anchorPointList[4]->setParentItem(this);
}
RectItem::~RectItem()
{
}
2.2 重写boundingRect和paint函数
boundingRect是自定义图元的活动范围,如果设置错误,拖动锚点将没有反应。
paint是用来绘制图元的。
QRectF RectItem::boundingRect()const
{
return QRect(m_rect.x() - 10, m_rect.y() - 10, m_rect.width() + 20, m_rect.height() + 20);
}
void RectItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
painter->setPen(Qt::red);
painter->setBrush(Qt::blue);
painter->drawRect(m_rect);
}
2.2 重写ItemBase的updateNewPosition函数
重写updateNewPosition,用于接收锚点图元的新坐标,并触发重绘事件。
void RectItem::updateNewPosition(QPointF p, AnchorType type)
{
m_point = p;
m_anchorType = type;
switch (m_anchorType)
{
case LU:
{
this->setLU(m_point);
scene()->update();
break;
}
case LD:
{
this->setLD(m_point);
scene()->update();
break;
}
case RU:
{
this->setRU(m_point);
scene()->update();
break;
}
case RD:
{
this->setRD(m_point);
scene()->update();
break;
}
case CENTER:
{
this->setCENTER(m_point);
scene()->update();
break;
}
default:
break;
}
}
void RectItem::setLU(QPointF p)
{
m_width = m_x - p.x() + m_width;
m_height = m_y - p.y() + m_height;
m_x = p.x();
m_y = p.y();
m_rect = QRectF(m_x, m_y, m_width, m_height);
m_anchorPointList[0]->setPoint(QPointF(m_x, m_y));
m_anchorPointList[1]->setPoint(QPointF(m_x, m_y + m_height));
m_anchorPointList[2]->setPoint(QPointF(m_x + m_width, m_y));
m_anchorPointList[3]->setPoint(QPointF(m_x + m_width, m_y + m_height));
m_anchorPointList[4]->setPoint(QPointF(m_x + m_width / 2., m_y + m_height / 2.));
}
void RectItem::setLD(QPointF p)
{
m_width = m_x - p.x() + m_width;
m_height = -m_y + p.y();
m_x = p.x();
m_rect = QRectF(m_x, m_y, m_width, m_height);
m_anchorPointList[0]->setPoint(QPointF(m_x, m_y));
m_anchorPointList[1]->setPoint(QPointF(m_x, m_y + m_height));
m_anchorPointList[2]->setPoint(QPointF(m_x + m_width, m_y));
m_anchorPointList[3]->setPoint(QPointF(m_x + m_width, m_y + m_height));
m_anchorPointList[4]->setPoint(QPointF(m_x + m_width / 2., m_y + m_height / 2.));
}
void RectItem::setRU(QPointF p)
{
m_width = -m_x + p.x();
m_height = m_y - p.y() + m_height;
m_y = p.y();
m_rect = QRectF(m_x, m_y, m_width, m_height);
m_anchorPointList[0]->setPoint(QPointF(m_x, m_y));
m_anchorPointList[1]->setPoint(QPointF(m_x, m_y + m_height));
m_anchorPointList[2]->setPoint(QPointF(m_x + m_width, m_y));
m_anchorPointList[3]->setPoint(QPointF(m_x + m_width, m_y + m_height));
m_anchorPointList[4]->setPoint(QPointF(m_x + m_width / 2., m_y + m_height / 2.));
}
void RectItem::setRD(QPointF p)
{
m_width = -m_x + p.x();
m_height = -m_y + p.y();
m_rect = QRectF(m_x, m_y, m_width, m_height);
m_anchorPointList[0]->setPoint(QPointF(m_x, m_y));
m_anchorPointList[1]->setPoint(QPointF(m_x, m_y + m_height));
m_anchorPointList[2]->setPoint(QPointF(m_x + m_width, m_y));
m_anchorPointList[3]->setPoint(QPointF(m_x + m_width, m_y + m_height));
m_anchorPointList[4]->setPoint(QPointF(m_x + m_width / 2., m_y + m_height / 2.));
}
void RectItem::setCENTER(QPointF p)
{
m_x = p.x() - m_width / 2.;
m_y = p.y() - m_height / 2.;
m_rect = QRect(m_x, m_y, m_width, m_height);
m_center = QPointF(m_x + m_height / 2., m_y + m_width / 2.);
m_anchorPointList[0]->setPoint(QPointF(m_x, m_y));
m_anchorPointList[1]->setPoint(QPointF(m_x, m_y + m_height));
m_anchorPointList[2]->setPoint(QPointF(m_x + m_width, m_y));
m_anchorPointList[3]->setPoint(QPointF(m_x + m_width, m_y + m_height));
m_anchorPointList[4]->setPoint(QPointF(m_x + m_width / 2., m_y + m_height / 2.));
}
3.锚点图元实现
锚点图元非常重要,它负责重绘或移动图元。当锚点是四个角时,移动锚点就会重新自定义图元。当锚点是中心位置时,就会移动图元。
3.1 重写boundingRect和paint
paint根据锚点类型绘制相应的形状
QRectF AnchorPoint::boundingRect() const
{
return QRectF(m_point.x() - m_drawSize / 2., m_point.y() - m_drawSize / 2., m_drawSize, m_drawSize);
}
void AnchorPoint::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
m_scaleFactor = painter->transform().m11();
switch (m_anchorType)
{
case LU:
case LD:
case RU:
case RD:
{
painter->setPen(Qt::darkMagenta);
painter->drawRect(m_point.x() - m_drawSize / 2. / m_scaleFactor, m_point.y() - m_drawSize / 2. / m_scaleFactor, m_drawSize / m_scaleFactor, m_drawSize / m_scaleFactor);
break;
}
case CENTER:
case ROTATE:
{
painter->setPen(Qt::black);
painter->drawEllipse(m_point.x() - m_drawSize / 2. / m_scaleFactor, m_point.y() - m_drawSize / 2. / m_scaleFactor, m_drawSize / m_scaleFactor, m_drawSize / m_scaleFactor);
break;
}
default:
break;
}
}
3.2 重写鼠标事件
鼠标事件是为了获取鼠标的坐标,其中MouseMoveEvent是获取鼠标移动的距离,然后重绘锚点图元,并将新坐标发送给父图元重绘自定义图元。
void AnchorPoint::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
QGraphicsItem::mouseMoveEvent(event);
if (event->buttons() == Qt::LeftButton)
{
ItemBase* item = static_cast<ItemBase*>(this->parentItem());//父类对象指向子类对象
m_itemType = item->m_itemType;
switch (m_itemType)
{
case ItemBase::Rect:
{
#pragma region 矩形
switch (m_anchorType)
{
case ItemBase::LU:
{
setCursor(Qt::ClosedHandCursor);
qreal dx = event->scenePos().x() - event->lastScenePos().x();
qreal dy = event->scenePos().y() - event->lastScenePos().y();
m_point += QPoint(dx, dy);
item->updateNewPosition(m_point, ItemBase::LU);
break;
}
case LD:
{
setCursor(Qt::ClosedHandCursor);
qreal dx = event->scenePos().x() - event->lastScenePos().x();
qreal dy = event->scenePos().y() - event->lastScenePos().y();
m_point += QPoint(dx, dy);
item->updateNewPosition(m_point, ItemBase::LD);
break;
}
case RU:
{
setCursor(Qt::ClosedHandCursor);
qreal dx = event->scenePos().x() - event->lastScenePos().x();
qreal dy = event->scenePos().y() - event->lastScenePos().y();
m_point += QPoint(dx, dy);
item->updateNewPosition(m_point, ItemBase::RU);
break;
}
case RD:
{
setCursor(Qt::ClosedHandCursor);
qreal dx = event->scenePos().x() - event->lastScenePos().x();
qreal dy = event->scenePos().y() - event->lastScenePos().y();
m_point += QPoint(dx, dy);
item->updateNewPosition(m_point, ItemBase::RD);
break;
}
case CENTER:
{
setCursor(Qt::ClosedHandCursor);
qreal dx = event->scenePos().x() - event->lastScenePos().x();
qreal dy = event->scenePos().y() - event->lastScenePos().y();
m_point += QPointF(dx, dy);
item->updateNewPosition(m_point, ItemBase::CENTER);
break;
}
}
#pragma endregion
break;
}
}
}
}
}
void AnchorPoint::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
setCursor(Qt::ArrowCursor);
}
更新于2024.10.1