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);
}
相关推荐
wanghowie12 小时前
02.04.01 Java Stream API 进阶指南:从底层实现到性能优化
java·开发语言·性能优化
怪我冷i12 小时前
dbeaver如何连接PostgreSQL数据库
数据库·ai编程·ai写作
superman超哥12 小时前
仓颉元编程进阶:编译期计算能力的原理与深度实践
开发语言·后端·仓颉编程语言·仓颉·仓颉语言·仓颉元编程·编译器计算能力
QH_ShareHub12 小时前
如何使用 NHANES 数据库
数据库
wuhen_n12 小时前
系统架构设计师(三):数据库系统
数据库·系统架构
这周也會开心13 小时前
Map集合的比较
java·开发语言·jvm
DB虚空行者13 小时前
MySQL误删/批量更新数据恢复实战:基于Flashback工具的完整方案
数据库·mysql
IvorySQL13 小时前
外键的本质竟然是触发器?深入解析 PostgreSQL 约束底层
数据库·postgresql·开源
挖矿大亨13 小时前
C++中的赋值运算符重载
开发语言·c++·算法
superman超哥13 小时前
Rust 基本数据类型:类型安全的底层探索
开发语言·rust·rust基本数据类型·rust底层探索·类型安全