Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Help with QGraphicsSvgItem
ncnai last edited by
I am trying to display a svg image using QGraphicsSvgItem but what comes out is very different than what is expected.
Here is the code I currently have to attempt displaying the svg file:
QGraphicsScene *scene = new QGraphicsScene(); QGraphicsSvgItem *svg = new QGraphicsSvgItem(QString("brackets.svg")); scene->addItem(svg); ui->graphicsView->setScene(scene); ui->graphicsView->show();
Contents of brackets.svg: https://pastebin.com/90AYtckw (can verify the expected output here: https://www.rapidtables.com/web/tools/svg-viewer-editor.html just copy paste the contents and click the magnifying glass to view)
What is expected:
What I get from the above code in the graphicsView:
Am I missing a step in my code to get this svg file to display properly? I have looked around but cannot tell if there is something wrong with my code or I am missing something.
Thanks for your help.
Hi and welcome to the forums
What are they grey rects ?
Do you insert any other items besides the QGraphicsSvgItem ?
It kinda looks it just behind the grey ones.
Items have zorder which is determined by the insert order unless changed via
So if you do insert say some Rect items, just insert SVG last and it will be on top.
ncnai last edited by ncnai
Hello and thank you,
No, the QGraphicsSvgItem is the only item I have added to the scene.
Just to check, I called
scene->clear()before I added the QGraphicsSvgItem but the result is the same.
Ahh, its all inside the SVG. ( just viewed it)
Maybe it uses features Qt SVG do not support. Qt SVG is is not latest revision.
I will try to export it from inkscape and see.
Hmm. Inkscape also find the SVG odd. its seems to uses groups/layers in some odd way.
I cant edit all elements and even ungrouping do not help.
Ok so its just in layers/combined objects.
How/what app did you draw it ?
I think you need to make a simpler/other version to have it drawn properly in Qt.
If i collapse the layers in Inkscape it get a bit messy just like Qt shows it.
So I think you need a more flat version :)
ncnai last edited by
Ahh I see, thank you for the information! I am not too familiar at working with SVG. The file I obtained was downloaded off of Challonge so I am not too sure how they are drawing it exactly, but if Qt is having trouble drawing it properly I will try to seek out some alternatives.
Also as a fun side note I tried loading the file through a QXmlStreamReader, passing the reader to a QSvgRenderer, and then sharing it with my QGraphicsSvgItem. What came out was the Challonge logo (the top part of what was expected) but nothing else.
Anyways thanks for your help :)
Yeah i also fooled around with it. it seems Qt do not understand the
sub objects. For fun , i recreated same layout using boxes and Qt had no issues
with that so it should be possible to do as you want with a less advanced version.
You can use inkscape (free software) to create a new version with same look.
Should not be that complicated. Inkscape also acted a bit odd on that file as
it would sort of collapse if i copy & pasted to new sheet. Not seen that before.