PDA

View Full Version : Drawing QGraphicsSvgItem from center



Mhondoz
14th March 2008, 14:34
Hi.

I have an application using QGraphicsView, where I can click in the view to add an SVG item. I want this item to be drawn with the center of the image at the position I click. Right now I get the scene position from the click, and set that as the new items position, but then the item seem to be drawn with this position as its upper left corner...

Is it something I can do in the SVG file (for example using Inkscape) to let the svg items position be the center of the image, or in the paint method of the item perhaps?

I guess I could calculate some offset based on the image size and adjust the position based on that, but I hope to avoid that solutions since I would like the item->pos() to represent the actual position.

wysota
14th March 2008, 15:19
You can subclass the item and reimplement boundingRect() and paint().

aamer4yu
14th March 2008, 15:20
when setting the item position,,, why cant u set it like this..

say scenePos = mouse position in scene.
itemPos = scenePos.adjusted(-item.width/2 , -item.height/2);
u can manipulate the itemposition in this way. seems u are just setting the position of item at scene position of mouse click.
dont forget to check the boundary lies in scene or not :)
Hope it helps :)

Mhondoz
14th March 2008, 21:33
@wysota: Yes I know, was just hoping for some easier solution here... Like in Incscape I can set an offset for the graphics so the top left part has negative coordinates and the center of the image is 0,0.

@aamer4yu: I was trying to avoid to change the position of the item because I may want to use it later on by asking the item for its position (which is lat/lon in a real world map), but I see now that this might not be a problem...

Anyway, thanks to both of you for the replies. :)