I am trying to display the contents of a .svg file in a QGraphicsView in Qt. For this I use:
QImage image (SCENEWIDTH, SCENEHEIGHT, QImage :: Format_ARGB32);
QSvgRenderer * renderer = new QSvgRenderer (path_img); //path of .svg file
QPainter painter (& image);
renderer-> render (& painter);
QGraphicsSvgItem * f = new QGraphicsSvgItem ();
f-> setSharedRenderer (renderer);
scene2-> setSceneRect (QRectF (0, 0, SCENEWIDTH, SCENEHEIGHT))
scene2-> addItem (f);
ui-> view-> setScene (scene2);
ui-> view-> show ();
The content of the svg consists of several lines to draw that are painted over a background image, loaded into the svg using:
<image id = "img1" href = "sample.png" />
The sample.png image is in the same path as the .svg file I want to display. The browser paints the image correctly if I open it directly from the browser, but nevertheless in the QGraphicsView only the lines are painted, but not the background image.
I have tried many options from searches, but I can not find the reason why this is happening. Thanks for the help
This is a sample SVG file, the image sakura.jpg should be in the same directory as svg file:
<svg xmlns="http://www.w3.org/2000/svg" version="1.0" style="background-color:black" width="1920" height="1080">
<style>
.blue { stroke:blue; stroke-width:9; fill:transparent; }
.red { stroke:red; stroke-width:10; fill:transparent;}
</style>
<g class="zone05" transform="translate(0,0) rotate(0,0,0) scale(1)">
<image id="img05" href="sakura.jpg"/>
<path class="red" d="M 851 444 m -19 -19 l 38 38 m 0 -38 l -38 38" />
<path class="red" d="M 475 520 m -19 -19 l 38 38 m 0 -38 l -38 38" />
<path class="red" d="M 1047 790 m -19 -19 l 38 38 m 0 -38 l -38 38" />
<path class="blue" d="M 1270 638 m -19 -19 l 38 38 m 0 -38 l -38 38" />
</g>
</svg>
I try your code and it works but the only things that I added to your code, I put
svgfile path insideQStringand I addQGraphicsScenethat I'm sure you have this too. I push it in this repo for you to test it :https://bitbucket.org/parisa_HR/stackoverflow/src/master/
updated :
I changed my SVG file and add one
jpegfile as an example of background.but it works.
I also try png too.
As File that you send :