如何获得翻译的QPolygon的终点

时间:2015-02-05 14:15:58

标签: qt qpainter 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);

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

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

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);

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