0

矢印を描画しようとしているので、矢印を描画できるサンプル コードを参照しました。

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()値を変更するにはどうすればよいですか? 矢印の頭が描かれた後に線を開始する必要があります。その理由は、ペンの幅が大きくなると、矢印の頭が線よりも小さく見えることがあります。theCurrentLinepolygon(arrowHead)

4

2 に答える 2

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

矢印の頭を色で塗りつぶしたい場合は、QPolygon の代わりに QPainterPath を使用する必要があります。

 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-06T08:16:43.827 に答える
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);

誰かがこれを実装するためのより良い方法を知っている場合 (私はあると確信しています)、私を修正してください。

于 2015-02-06T10:11:56.487 に答える