我试图绘制一个箭头,所以我刚才引用了示例代码,在这里我们可以绘制箭头:
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);但是现在我想在箭头多边形画出后画出这条线。
如何更改theCurrentLine的theCurrentLine值,该值可以在多边形之后开始,如当前的polygon(arrowHead)和行开始于同一点?我需要在箭头画好后开始画线。原因是,有时,如果钢笔宽度增加,箭头头看起来比线小。
发布于 2015-02-06 08:16:43
您可以在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-06 10:11:56
我的实际执行情况:
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);如果有人知道更好的方法来实现这一点(我肯定会有),请纠正我。
https://stackoverflow.com/questions/28346259
复制相似问题