首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获得翻译后的QPolygon的端点

如何获得翻译后的QPolygon的端点
EN

Stack Overflow用户
提问于 2015-02-05 14:15:57
回答 2查看 1.4K关注 0票数 0

我试图绘制一个箭头,所以我刚才引用了示例代码,在这里我们可以绘制箭头:

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

我决定使用同样的公式绘制,并尝试如下:

代码语言:javascript
复制
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);

但是现在我想在箭头多边形画出后画出这条线。

如何更改theCurrentLinetheCurrentLine值,该值可以在多边形之后开始,如当前的polygon(arrowHead)和行开始于同一点?我需要在箭头画好后开始画线。原因是,有时,如果钢笔宽度增加,箭头头看起来比线小。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-02-06 08:16:43

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

代码语言:javascript
复制
 QPoint QPolygon::point ( int index ) const

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

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

代码语言:javascript
复制
 QPoint lastPoint = myPolygon.point(myPolygon.count() - 1);

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

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

代码语言:javascript
复制
 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);

您需要在最后一点和第一个点之间划定界限。在此:

代码语言:javascript
复制
 p->drawLine(myPolygon.point(0, myPolygon.point(myPolygon.count() - 1));

如果希望箭头头部充满颜色,则需要使用QPainterPath而不是QPolygon:

代码语言:javascript
复制
 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
EN

Stack Overflow用户

发布于 2015-02-06 10:11:56

我的实际执行情况:

代码语言:javascript
复制
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);

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28346259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档