[Qt-jambi-interest] Problems controlling SVG object location
Gunnar Sletta
gunnar at trolltech.com
Mon Aug 11 08:44:59 CEST 2008
abigbee wrote:
> Hi,
>
> I'm having difficulty controlling where SVG objects/images are
> rendered in QImages that I save to disk. Not sure if there is a
> problem with the SVG file or whether I am misunderstanding the
> coordinate systems in QT Jambi 4.4.
>
> To simply testing, I've created a rectangle in a 100x100 file (see
> below). This file is loaded as a QGraphicsSvgItem and rendered with
> QSvgRenderer with a QGraphicsView parent using a QGraphicsScene.
> I create 500x500 QImage, and a QPainter is constructed with the QImage.
>
> The end result is that the 10x100 rectangle ends up in the middle of
> the 500x500 png image when I look at the resulting file. Based on
> QGraphicsSvgItem.pos() being 0,0, I would expect it to be rendered in
> the upper left of the image. If 0,0 in the scene is the center of the
> image, I would expect the top of the rectangle to start at the origin
> in the cener of the image and extend down instead of extending below
> and above the image. When using QPainter.drawXXX to draw primitives,
> the primitives themselves are rendered with 0,0 being the upper left
> part of the image.
>
> When I scale and rotate svg objects, I can't accurately predict where
> the center of the rotated shape is, despite the original svg object
> being symmetrical. I basically want to be able to place the object
> anywhere in the image and rotate it about the center of the object
> itself.
>
> Below the svg is the ugly test code I'm using to investigate--you can
> see I try all sorts of things.
Hi,
What you are bitten by here is the cleverness of QGraphicsView ;-)
As a convenience, when you don't specify a scene rectangle,
QGraphicsScene will calculate one for you, and when you don't set a
scenerect on the QGraphicsView it will calcuate one that it thinks will
make sense, putting the content in the center of the view. From the
perspective that this is scene rendering, not pixel drawing, this is the
right thing to do. You can remedy this by forcing the sceneRect to be
the dimension of the target image and similarly setting the sceneRect in
the view to be the size of the image.
Regarding the item pos at 0,0, this is the items position in the item
coordinate space. Its position in the scene can be different depending
on the items transformation and the scene's transformation. See more
about graphicsview coordinate systems here:
http://doc.trolltech.com/qtjambi-4.4/html/com/trolltech/qt/graphicsview.html#the-graphics-view-coordinate-system
If your end goal is to just rotate the svg, then QGraphicsView is
overkill. You should just use QPainter directly on a image.
best regards,
Gunnar
More information about the Qt-jambi-interest
mailing list