QT在控件graphicsView中绘制箭头

这里写自定义目录标题

前言:

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

基础夯实:

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

成功效果展示:

失败效果展示:

核心代码:

cpp 复制代码
#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 startPoint

        switch (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);
}
相关推荐
GISer_Qing2 分钟前
ASP.NET Core 8.0学习笔记(二十七)——数据迁移:Migrations深入与其他迁移命令
数据库·c#·.netcore·entityframework
蓝桉80218 分钟前
图片爬取案例
开发语言·数据库·python
逸狼24 分钟前
【JavaEE进阶】Spring DI
java·开发语言
Ljw...32 分钟前
DeepSeek+Kimi生成高质量PPT
数据库·c++·powerpoint·ppt·deepseek
m0_7482405439 分钟前
数据库操作与数据管理——Rust 与 SQLite 的集成
数据库·rust·sqlite
生产队队长41 分钟前
ThinkPHP:配置Redis并使用
android·数据库·redis
my_styles1 小时前
2025-alibaba-Sentinel组件
java·开发语言·sentinel
致奋斗的我们1 小时前
HAProxy介绍与编译安装
linux·汇编·数据库·mysql·青少年编程·haproxy·openeurler
禁默1 小时前
C++之旅-C++11的深度剖析(1)
开发语言·c++
偏右右1 小时前
UNION 联合查询
数据库·sql