views:

44

answers:

2

Hi guys

i want to draw triangler object in graphicsview by using qgraphicsitem. But I dont know how to implement bounding rect according to triangler.

A: 

To draw triangle you need 3 points and draw the line between them, subclass the graphicsitem and in the paint method of subclass class draw triangle later set the item to Graphicsscene then add the scene to graphicsview.

Shadow
+2  A: 

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.

Jérôme
but how can i detect collision ?
ibrahimyilmaz
@ibrahimyilmaz : have a look at [QGraphicsScene::collidesWithItem()](http://doc.trolltech.com/4.6/qgraphicsitem.html#collidesWithItem)
Jérôme