PDA

View Full Version : Centralizing various GraphicsItem in QGraphicsView



ranna
16th June 2009, 09:14
Hi,

I am trying to draw a QPolygon of QVectors in a QGraphicsScene.

Depends on some conditions, n numbher of polygon is added in the graphicsview and it has been drawn in different places of the view... like first polygon is placed in left bottom of the QGV, second is at right bottom, etc in a random manner...

I would like to place all the polygons in a central axis like in the After_Centralization.jpg image... (Rfer Before_Centralization)...

Please help me on acheiving this?

Regards
Ranna

wysota
16th June 2009, 09:39
But what exactly is the problem? Just set the position of the items using QGraphicsItem::setPos().

ranna
16th June 2009, 10:30
The problem scenario is below:

1. A list of polygons are displayed in the left side of the widget.
2. On the right side of the widget a preview area is there with QGraphicsView widget..
3. The user can select a polygon from the left side and the selected polygon is displayed in
the right side preview area...
4. The selected polygons are scattered at different points of QGraphicsView.
5. I require that the polygons should be drawn in correct order as you can see in the attachement AFter_Centralization.jpg.

I am adding the polygon like below


DrawPolygon(int xOffset, int yOffset)
{
QGraphicsView *ptrGraphicsView = new QGraphicsView();
QGraphicsScene *polyScene = new QGraphicsScene();
QVector <QPointF> vctPoints;
for(int i=xOffset; i<10; i++)
{
for(j=yOffset; j<10; j++)
{
vctPoints.append(QPointF(i*50,j*50));
}
}
QPolygon *polygon = new QPolygon(vctPoints);
polyScene->addPolygon(polygon);
ptrGraphisView->setScene(polyScene);
}


The above function draws polygon considering different offset values... and so the polygons get scattered in the QGV...

I want to place the scattered polygons in a central axis...

Regards
Ranna

wysota
16th June 2009, 11:50
You can use QGraphicsItem::moveBy() to move the items in the horizontal axis. Just take the boundingRect() of the item, and calculate the offset between its centre and the centre of your scene and pass that value to moveBy().