Qt绘制箭头
接收两个点作为向量的两端,用来确定箭头方向,额外添加参数,确定箭头是否和向量一致
然后使用fillPath
填充箭头
QPainterPath arrowPath(const QPointF& p1, const QPointF& p2, int direct = 0, int endIndex = 0, int arrowSize = 10){QPainterPath path;QLineF line(p1, p2);double angle = ::acos(line.dx() / line.length());if (line.dy() >= 0)angle = (M_PI * 2) - angle;if (endIndex == 0 || endIndex == 1){if (direct == 0){QPointF arrowP1 = p1 + QPointF(sin(angle + M_PI / 3) * arrowSize,cos(angle + M_PI / 3) * arrowSize);QPointF arrowP2 = p1 + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,cos(angle + M_PI - M_PI / 3) * arrowSize);path.moveTo(p1);path.lineTo(arrowP1);path.lineTo(arrowP2);path.lineTo(p1);}else{QPointF arrowP1 = p1 - QPointF(sin(angle + M_PI / 3) * arrowSize,cos(angle + M_PI / 3) * arrowSize);QPointF arrowP2 = p1 - QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,cos(angle + M_PI - M_PI / 3) * arrowSize);QPointF extendP1 = p1 - QPointF(sin(angle) * 2 * arrowSize,cos(angle) * 2 * arrowSize);path.moveTo(p1);path.lineTo(arrowP1);path.lineTo(arrowP2);path.lineTo(p1);path.lineTo(extendP1);}}if (endIndex == 0 || endIndex == 2){if (direct == 0){QPointF arrowP1 = p2 - QPointF(sin(angle + M_PI / 3) * arrowSize,cos(angle + M_PI / 3) * arrowSize);QPointF arrowP2 = p2 - QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,cos(angle + M_PI - M_PI / 3) * arrowSize);path.moveTo(p2);path.lineTo(arrowP1);path.lineTo(arrowP2);path.lineTo(p2);}else{QPointF arrowP1 = p2 + QPointF(sin(angle + M_PI / 3) * arrowSize,cos(angle + M_PI / 3) * arrowSize);QPointF arrowP2 = p2 + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,cos(angle + M_PI - M_PI / 3) * arrowSize);QPointF extendP2 = p2 + QPointF(sin(angle) * 2 * arrowSize,cos(angle) * 2 * arrowSize);path.moveTo(p2);path.lineTo(arrowP1);path.lineTo(arrowP2);path.lineTo(p2);path.lineTo(extendP2);}}return path;}