ibrahimyilmaz
ibrahimyilmaz

Reputation: 18929

How to draw a triangle by using QGraphicsView's QGraphicsItem class

I want to draw a triangular object in QGraphicsView by using QGraphicsItem. But I don't know how to implement bounding rect according to triangler.

Upvotes: 3

Views: 6715

Answers (2)

Jérôme
Jérôme

Reputation: 27047

You could use a QGraphicsPolygonItem.

You just have to describe a triangle polygon with QPolygonF and then add it to your scene with QGraphicsScene::addPolygon().

// Describe a closed triangle
QPolygonF Triangle;
Triangle.append(QPointF(-10.,0));
Triangle.append(QPointF(0.,-10));
Triangle.append(QPointF(10.,0));
Triangle.append(QPointF(-10.,0));

// Add the triangle polygon to the scene
QGraphicsPolygonItem* pTriangleItem = pScene->addPolygon(Triangle);

This way, everything is handled by Qt, you don't have to worry about bounding rect.

Upvotes: 9

Naruto
Naruto

Reputation: 9634

To draw triangle you need 3 points and draw the line between them. Subclass the QGraphicsItem and in the paint method of subclass class draw triangle later set the item to QGraphicsScene then add the scene to QGraphicsView.

Upvotes: 0

Related Questions