[Qt-jambi-interest] Problems controlling SVG object location

abigbee tony.bigbee at mac.com
Sun Aug 10 03:54:18 CEST 2008


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.

Thanks for any suggestions.

Relevant parts of the .svg file:
<svg
    width="100"
    height="100"
<g
      id="hull"
      inkscape:label="Layer 1"
      inkscape:groupmode="layer"
      >
     <rect
        style="fill:#0000ff;fill-opacity:1;stroke:#000000;stroke- 
opacity:1"
        id="rect2410"
        width="10"
        height="100"
        x="0"
        y="0" />
   </g>



import com.trolltech.qt.gui.*;
import com.trolltech.qt.svg.QSvgRenderer;
import com.trolltech.qt.core.Qt.BrushStyle;
import com.trolltech.qt.svg.QGraphicsSvgItem;
import com.trolltech.qt.core.Qt.Axis;
import com.trolltech.qt.core.QPointF;
import com.trolltech.qt.core.QRectF;


public class Tester {

     public static final String air_sym = "classpath:images/test.svg";
     private QSvgRenderer renderer;

     public Tester() {
         QGraphicsScene scene = new QGraphicsScene();
         QGraphicsView view = new QGraphicsView();
         view.setScene(scene);
         view.setRenderHint(QPainter.RenderHint.Antialiasing);
         QGraphicsSvgItem item = new QGraphicsSvgItem();
         renderer = new QSvgRenderer(air_sym,view);
         item.setElementId("hull");
         item.setParent(renderer);
         item.setSharedRenderer(renderer);
         scene.addItem(item);
         QTransform transform = new QTransform();
         //System.out.println(item.elementId());
         //System.out.println(renderer.isValid());
         QImage image = new QImage(500,500,QImage.Format.Format_RGB32);
         QPainter painter = new QPainter(image);
         painter.setRenderHint(QPainter.RenderHint.Antialiasing);
         painter.setPen(new QPen(QColor.white));
         QBrush brush = new QBrush(BrushStyle.SolidPattern);
         brush.setColor(QColor.white);
         painter.setBrush(brush);
	QPen pen = new QPen(QColor.lightGray);
         pen.setWidth(3);
         painter.setPen(pen);
         QPointF point = new QPointF(10.0d,10.0d);
         painter.drawEllipse(point,20.0d,20.0d);
         //transform.scale(.3,.3);
         //item.setTransform(transform,false);
         System.out.println(item.scenePos());
	QRectF rect = item.sceneBoundingRect();
	System.out.println("item.sceneBoundingRect() + " + rect);
	double width = rect.width();
         double height = rect.height();
         double top = rect.top();
         double left = rect.top();
         System.out.println("height " + height + " width " + width);
         System.out.println("scene pos " + item.scenePos());
         /* This is supposedly how we rotate around the center of the  
item
         transform.translate(width/2,height/2);
         transform.rotate(90);
         transform.translate(-width/2,-height/2);
         item.setTransform(transform,false);
         */
         System.out.println("scene pos " + item.scenePos());
         System.out.println(item.sceneBoundingRect());
         System.out.println(item.mapToParent(width/2,height/2));
         QPointF center = item.sceneTransform().map(new QPointF(width/ 
2,height/2));
         System.out.println("item.sceneTransform().map((width/2,height/ 
2)) -> " + cente\
r);
         System.out.println(item.sceneTransform().map(new  
QPointF(0,0)));
         pen.setColor(QColor.blue);
         brush.setColor(QColor.blue);
         painter.setBrush(brush);
         painter.drawEllipse(center,5.0d,5.0d);
         System.out.println(item.pos());
         System.out.println(center);
         System.out.println("mapFromScene(item.pos()) " +  
item.mapFromScene(item.pos())
         );
         view.render(painter);
         //QImage image2 = image.copy(20,20,60,60);
         image.save("aircraft.png","PNG");
         painter.end();
     }

     public static void main(String args[]) {
         QApplication.initialize(args);
         Tester tester = new Tester();
         System.exit(0);
         //QApplication.exec();
     }
}




More information about the Qt-jambi-interest mailing list