PDA

View Full Version : QGraphicsSvgItem sizing



jefferai
27th June 2007, 17:58
I have a class derived from QGraphicsSvgItem where I implement boundingRect as a function of the sceneRect()'s width and height. I want the SVG graphic to scale to fill the boundingRect, but I can't seem to get the trick. How could I cause the SVG graphic (which is smaller than the boundingRect) to fill the boundingRect (keeping its original aspect)?

I know you can scale() it, but I'm not sure how I'd discover how *much* to scale it, if that's even the right approach.

Thanks.

marcel
27th June 2007, 21:48
I am not sure if it is possible to always keep the aspect ratio after scaling.
If the aspect ratio of the scene rect is not equal with the aspect ratio of item's bounding box, then you're out of luck.

Generally, the scale factors are as follows:


float sx = sceneRect.width()/boundingBox.width();
float sy = sceneRect.height()/boundingBox.height();


Next, you must call:


svgItem->scale( sx, sy );


There might be needed a repositioning( to center the item in view), but only in practice you can see if it is really necessary.

If the aspect ratios don't match, and if you find it acceptable, you can pad the item with a certain color or pattern in the area where it does not fill the scene rect.

Regards