PDA

View Full Version : Adding QGraphicItems to QGraphicScene causes the previous QGraphicItem to move. Why??



kapoorsudhish
12th March 2010, 08:47
Hi,
I am working with QGraphicView, QGraphicScene and QGraphicItem to draw various rectangles on the scene. But once i add the item to the scene, causes the previous QGraphicItem to move, which is not required. Can some one please help me with this as to how can i stop the movement of the existing Items in scene while adding the new item.

regards,
sudhish

Lykurg
12th March 2010, 08:57
Can you provide a minimal example on how you add the items to the scene since I never have notized such a behavior.

kapoorsudhish
12th March 2010, 09:11
I am drawing the rectangle on the mouse release event using the following code, please let me know what im doing wrong



void GraphicsScene::mouseReleaseEvent(QGraphicsSceneMou seEvent *mouseEvent)
{
static int rectCount;
if(!selectionArea().isEmpty())
{
if(rectCount<7)
{
GraphicsRectItem *item = new GraphicsRectItem(selectionArea().boundingRect());
addItem(item);
rectCount ++;
}
}
QGraphicsScene::mouseReleaseEvent(mouseEvent);
}


where the GraphicScene and GraphicsRectItem classes are derived from QT QGraphicScene and QGraphicsRectItem as


class GraphicScene : public QGraphicsScene
class GraphicsRectItem : public QGraphicsRectItem

Lykurg
12th March 2010, 09:57
You don't position the item so I guess you have centered your scene in the view? Is that right? If so, positioning the scene at top left will stop moving your existing items around.

kapoorsudhish
12th March 2010, 11:21
How can i position the item? As there is no function which allows me to add the GraphicItem at a position. My requirement is to draw various rectangles on the given scene. How can i achieve this??

Can you please help me with the functions which can be used.


regards,
sudhish

Lykurg
12th March 2010, 11:59
QGraphicsItem::setPos()

wysota
12th March 2010, 12:12
Hi,
I am working with QGraphicView, QGraphicScene and QGraphicItem to draw various rectangles on the scene. But once i add the item to the scene, causes the previous QGraphicItem to move, which is not required. Can some one please help me with this as to how can i stop the movement of the existing Items in scene while adding the new item.


Set a fixed size to the scene. If you forget to do it, the scene will expand to hold all items you place in it. If you add a new item, the scene expands and as the scene is centred in the view, other items will seem to "move" (in fact they won't, the scene will move in the view). See QGraphicsScene::setSceneRect()

kapoorsudhish
12th March 2010, 12:40
Hey Thanks wysota,


The selection rectangles does not "move" after setting the graphic view Scene size.
QGraphicsScene::setSceneRect()

Thanks for the help Lykurg.

regards,
sudhish