0

我正在尝试绘制箭头,所以我只是参考了可以绘制箭头的示例代码:

http://doc.qt.io/qt-5/qtwidgets-graphicsview-elasticnodes-edge-cpp.html

我决定使用相同的公式进行绘制并尝试如下:

theCurrentLine->setP1(QPointF(0, 0)  );
theCurrentLine->setP2((theLineVector));
p->drawLine(*theCurrentLine);

double angle = ::acos(theCurrentLine->dx() / theCurrentLine->length());
if (theCurrentLine->dy() >= 0)
    angle = TwoPi - angle;

QPointF sourcePoint = QPointF(0,0);

QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + Pi / 3) * theArrowSize,
                                              cos(angle + Pi / 3) * theArrowSize);
QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + Pi - Pi / 3) * theArrowSize,
                                              cos(angle + Pi - Pi / 3) * theArrowSize);

p->drawPolygon(QPolygonF() << theCurrentLine->p1() << sourceArrowP1 << sourceArrowP2);

但现在我想在绘制箭头多边形后画线。

如何更改可以在多边形之后开始的P1()值,theCurrentLine因为当前polygon(arrowHead)和线从同一点开始?我需要在绘制箭头后开始这条线。原因有时是如果笔宽增加箭头看起来比线小。

4

2 回答 2

1

您可以在 QPolygon 中的索引处获得点。

 QPoint QPolygon::point ( int index ) const

当您知道有多少点时,这将很容易。Qt 文档是您的朋友。

您可以使用 count(),例如:

 QPoint lastPoint = myPolygon.point(myPolygon.count() - 1);

只要给你的多边形起个名字,你会没事的。

编辑:这些代码的最新版本应该可以解决您的问题。我想我需要举个例子。您需要按以下顺序添加点:

 QPolygon myPolygon;
 myPolygon.setPoint(0, sourceArrowP1);
 myPolygon.setPoint(1, theCurrentLine->p1());
 myPolygon.setPoint(2, sourceArrowP2);
 p->drawPolygon(myPolygon);
 QPoint lastPoint;
 lastPoint = myPolygon.point(myPolygon.count() - 1);

您需要在最后一点和第一点之间画线。这里:

 p->drawLine(myPolygon.point(0, myPolygon.point(myPolygon.count() - 1));

如果你想让你的箭头用颜色填充,你需要使用 QPainterPath 而不是 QPolygon:

 QPen pen(Qt::black); //Or whatever color you want
 pen.setWidthF(10); //Or whatever size you want your lines to be
 QBrush brush(Qt::red); //Or whatever color you want to fill the arrow head
 p->setPen(pen);
 p->setBrush(brush);
 QPainterPath arrow(sourceArrowP1);
 arrow.lineTo(theCurrentLine->p1());
 arrow.lineTo(sourceArrowP2);
 arrow.lineTo(sourceArrowP1); //This returns to the first point. You might eliminate this line if you want to.
 p->drawPath(arrow);
于 2015-02-06T08:16:43.827 回答
0

我的实际实现:

theCurrentLine->setP1(QPointF(0, 0)  );  // arrow line coordinates
theCurrentLine->setP2((theLineVector));
double angle = ::acos(theCurrentLine->dx() / theCurrentLine->length());  // angle of the current Line
if (theCurrentLine->dy() >= 0)
    angle = TwoPi - angle;

// getting arrow head points to be drawn
QPointF sourcePoint = QPointF(0,0);

QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + Pi / 3) * theArrowSize,
                                              cos(angle + Pi / 3) * theArrowSize);
QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + Pi - Pi / 3) * theArrowSize,
                                              cos(angle + Pi - Pi / 3) * theArrowSize);

p->drawPolygon(QPolygonF() << theCurrentLine->p1() << sourceArrowP1 << sourceArrowP2);

// to find the center point in the arrow head right
QLineF perpLine = QLineF(theCurrentLine->p1(), theCurrentLine->p2());
QLineF arrowheadWidth = QLineF(sourceArrowP1, sourceArrowP2);

QPointF originPoint;
QLineF::IntersectType res = perpLine.intersect(arrowheadWidth, &originPoint);

theCurrentLine->setP1(originPoint);
p->drawLine(*theCurrentLine);

如果有人知道实现这一点的更好方法(我相信会有),请纠正我。

于 2015-02-06T10:11:56.487 回答