欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > QT在控件graphicsView中绘制箭头

QT在控件graphicsView中绘制箭头

2025/2/2 11:24:43 来源:https://blog.csdn.net/qq_54122113/article/details/141676277  浏览:    关键词:QT在控件graphicsView中绘制箭头

这里写自定义目录标题

  • 前言:
  • 基础夯实:
  • 成功效果展示:
  • 失败效果展示:
  • 核心代码:

前言:

对之前箭头没有成功绘制的补充,因为没有直接的箭头项,所以需要自己进行绘制

基础夯实:

可以直接看,建议看一下之前的绘制过程
在控件graphicsView中实现绘图功能(一)
在控件graphicsView中实现绘图功能(二)
在控件graphicsView中实现绘图功能(三)

成功效果展示:

在这里插入图片描述

失败效果展示:

在这里插入图片描述

核心代码:

#include "CustomGraphicsView.h"
#include <QGraphicsRectItem>
#include <QGraphicsScene>
#include <QMouseEvent>
#include <cmath>
#include <QPolygonF>CustomGraphicsView::CustomGraphicsView(QWidget *parent): QGraphicsView(parent), isDrawing(false), arrowPolygonItem(nullptr),arrowLineItem(nullptr)
{const double arrowSize = 10.0;
}void CustomGraphicsView::setDrawMode(DrawMode mode)
{currentDrawMode = mode;
}void CustomGraphicsView::mousePressEvent(QMouseEvent *event)
{if (event->button() == Qt::LeftButton) {isDrawing = true;startPoint = mapToScene(event->pos());endPoint = startPoint; // Initialize endPoint to startPointswitch (currentDrawMode) {case ArrowsMode:arrowPolygonItem = nullptr;arrowLineItem = nullptr; // 重置箭头直线项break;default:break;}emit mouseClicked(event->pos());}QGraphicsView::mousePressEvent(event);
}void CustomGraphicsView::mouseMoveEvent(QMouseEvent *event)
{if (isDrawing) {endPoint = mapToScene(event->pos());switch (currentDrawMode) {case ArrowsMode: {// 绘制直线if (!arrowLineItem) {arrowLineItem = scene()->addLine(QLineF(startPoint, endPoint), QPen(Qt::green));} else {arrowLineItem->setLine(QLineF(startPoint, endPoint));}// 绘制箭头头部if (!arrowPolygonItem) {// 计算从起点到终点的角度double angle = std::atan2(endPoint.y() - startPoint.y(), endPoint.x() - startPoint.x());// 调整角度,确保箭头是锐角(15度)double arrowAngle = M_PI - 15.0 / 180.0 * M_PI; // 15度角// 计算箭头的三个顶点QPointF arrowP1 = QPointF(endPoint.x() + 10.0 * std::cos(angle + arrowAngle), endPoint.y() + 10.0 * std::sin(angle + arrowAngle));QPointF arrowP2 = endPoint;QPointF arrowP3 = QPointF(endPoint.x() + 10.0 * std::cos(angle - arrowAngle), endPoint.y() + 10.0 * std::sin(angle - arrowAngle));QPolygonF arrowHeadPolygon;arrowHeadPolygon << arrowP1 << arrowP2 << arrowP3;arrowPolygonItem = scene()->addPolygon(arrowHeadPolygon, QPen(Qt::green), QBrush(Qt::green));} else {double angle = std::atan2(endPoint.y() - startPoint.y(), endPoint.x() - startPoint.x());double arrowAngle = M_PI - 15.0 / 180.0 * M_PI; // 15度角QPointF arrowP1 = QPointF(endPoint.x() + 10.0 * std::cos(angle + arrowAngle), endPoint.y() + 10.0 * std::sin(angle + arrowAngle));QPointF arrowP2 = endPoint;QPointF arrowP3 = QPointF(endPoint.x() + 10.0 * std::cos(angle - arrowAngle), endPoint.y() + 10.0 * std::sin(angle - arrowAngle));QPolygonF arrowHeadPolygon;arrowHeadPolygon << arrowP1 << arrowP2 << arrowP3;arrowPolygonItem->setPolygon(arrowHeadPolygon);}break;}default:break;}}emit mouseMoved(event->pos());
}void CustomGraphicsView::mouseReleaseEvent(QMouseEvent *event)
{if (event->button() == Qt::LeftButton && isDrawing) {isDrawing = false;}emit mouseReleased(event->pos());QGraphicsView::mouseReleaseEvent(event);
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com