4

How to open svg file like png file. For example it works for png file:

scene=new QGraphicsScene(QRect(10, 10, 680, 520));  
view=new QGraphicsView(this);
image=new QGraphicsPixmapItem(QPixmap("example.png"));

scene ->addItem(image);   
view ->setScene(scene);   
view ->setGeometry(QRect(270, 35, 700, 540));

Any ideas?

László Papp
  • 51,870
  • 39
  • 111
  • 135

1 Answers1

2

As per my comment, you could use QGraphicsSvgItem as per documentation:

QGraphicsSvgItem::QGraphicsSvgItem(const QString & fileName, QGraphicsItem * parent = 0)

Constructs a new item with the given parent and loads the contents of the SVG file with the specified fileName.

So, you would basically write something as follows:

 QGraphicsSvgItem *item = new QGraphicsSvgItem("example.svg");

You can also follow th example in the documentation if you wish to use it with the QSvgRenderer.

QSvgRenderer *renderer = new QSvgRenderer(QStringLiteral("example.svg"));
QGraphicsSvgItem *item = new QGraphicsSvgItem();

item->setSharedRenderer(renderer);
item->setElementId(QStringLiteral("example"));

Here you can even find a more sophisticated example of doing this:

SVG Viewer Example

Community
  • 1
  • 1
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • This is weird. I do as you wrote, but i got: "Cannot read file 'example.svg', because: (line 2)". When file doesnt exist it shows: "Cannot read file 'example.svg', because: No such file or directory" –  Jan 13 '14 at 20:17
  • @user3191398: can you open up the example.svg with an svn viewer, like inkscape or karbon, just to see if the svg is valid itself? – László Papp Jan 13 '14 at 20:20
  • I can open. It is valid –  Jan 13 '14 at 20:35
  • Perhaps the svg viewer corrects a recoverable error. Please upload the svg to somewhere, and provide a link to us. Try another svg files as well, just in case. – László Papp Jan 13 '14 at 20:36
  • @user3191398: as noted, the html should be changed at the top to xml to make it work. Perhaps, there is an explicit way of making it work, but this should get you going, at least. – László Papp Jan 13 '14 at 20:50
  • I changed html to xml and it doesnt work. When i delete and it works. Thanks a lot. –  Jan 13 '14 at 20:53