绘制一个简单的空心箭头。小封装方法。源码从此处抄袭而来,我只是完善了一下开头部分。https://www.pianshen.com/article/6539425025/
示例
源码
void drawCustomLine(QPainter * painter, QPointF startp, QPointF stopp, const QColor & color)
{
painter->save();
painter->setPen(QPen(color, 1.0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter->setBrush(QBrush(color));
float theta1 = std::atan2f(stopp.y() - startp.y(), stopp.x() - startp.x());
stopp = QPointF(stopp.x() - 10 * std::cosf(theta1), stopp.y() - 10 * std::sinf(theta1));
if (startp.isNull() == false && stopp.isNull() == false) {
QLineF line(startp, stopp);
painter->drawLine(line);
QLineF v = line.unitVector();
if (!v.isNull()) {
v.setLength(10);
v.translate(QPointF(line.dx(), line.dy()));
QLineF n = v.normalVector();
n.setLength(n.length() * 0.5);
QLineF n2 = n.normalVector().normalVector();
QPointF p1 = v.p2();
QPointF p2 = n.p2();
QPointF p3 = n2.p2();
//painter->setBrush(QBrush(color));
if (p1.isNull() == false && p2.isNull() == false) {
QLineF lineA(p1, p2);
if (lineA.length() > 4) {
painter->drawLine(lineA);
}
}
if (p2.isNull() == false && p3.isNull() == false) {
QLineF lineB(p2, p3);
if (lineB.length() > 4) {
painter->drawLine(lineB);
}
}
if (p3.isNull() == false && p1.isNull() == false) {
QLineF lineC(p3, p1);
if (lineC.length() > 4) {
painter->drawLine(lineC);
}
}
}
}
painter->restore();
}