I'm trying to draw an arrow so I just referred to the example code where we can draw arrows:
http://doc.qt.io/qt-5/qtwidgets-graphicsview-elasticnodes-edge-cpp.html
I decided to draw using the same formula and tried like:
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);
but now I want to draw the line after the arrow head polygon gets drawn.
How can I change the P1()
value of the theCurrentLine
which can start after the polygon as currently the polygon(arrowHead)
and the line start at the same point? I need to start the line after the arrow head is drawn. The reason is sometimes if the pen width increases the arrow head looks smaller than the line.
You can get the point at index in QPolygon.
It would be easy when you know how many points there are. And Qt documentation is your friend.
And you could use count(), for example:
Just give a name to your polygon, and you'll be fine.
Edit: Latest version of these codes should solve your problem. I thought I need to give an example. You need to add the points in this order:
You need to draw the line between last and first points. Here:
If you want your arrow head to be filled with color, you need to use QPainterPath instead of QPolygon: