#include <QPainter>
#include <math.h>
void DrawLineWithArrow(QPainter& painter, QPen pen, QPoint start, QPoint end)
{
painter.setRenderHint(QPainter::Antialiasing, true);
qreal arrowSize = 20;
painter.setPen(pen);
painter.setBrush(pen.color());
QLineF line(end, start);
double angle = std::atan2(-line.dy(), line.dx());
QPointF arrowP1 = line.p1() + QPointF(sin(angle + M_PI / 3) * arrowSize,
cos(angle + M_PI / 3) * arrowSize);
QPointF arrowP2 = line.p1() + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,
cos(angle + M_PI - M_PI / 3) * arrowSize);
QPolygonF arrowHead;
arrowHead.clear();
arrowHead << line.p1() << arrowP1 << arrowP2;
painter.drawLine(line);
painter.drawPolygon(arrowHead);
}
测试:
void MyWidget::paintEvent(QPaintEvent *)
{
QPainter paint(this);
QPen Pen = QPen(QColor(18, 157, 221), 2, Qt::SolidLine);
DrawLineWithArrow(paint, Pen, QPoint(0, 0), QPoint(300, 300));
}
结果: