如何获得翻译后的 QPolygon 的终点

how to get the end point of a translated QPolygon

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

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);

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

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

你可以在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);

我的实际实现:

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);

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