PDA

View Full Version : QSvgRenderer - correct rendering of svg-items despite of defaultSize()?



Songg
28th February 2016, 14:53
This is how i try to render SVG:


void DialogImageProperies::showSVG(QString sFile){
this->adjustSize();

QSvgRenderer rndr(sFile);
QPixmap px;
QSizeF szSvgF = rndr.defaultSize();

if (szSvgF.width() > szSvgF.height())
{
px = QPixmap(1000, int(qreal(1000 )* szSvgF.height()/szSvgF.width()));
}
else
{
px = QPixmap(int(qreal(1000 )* szSvgF.width()/szSvgF.height()), 1000 );
}


QPainter p(&px);
rndr.render(&p);

ui->wdgBck->setPixmap(px);
this->exec();
}

But as for me, i never notice what is a size of canvas in any created by myself vector-image. So, i think that users also will not predefine correct boundaries.

That is how SVG looks in Inkscape:
11750

That is what i get:
11749

For now i see no way to deal with all it...

ChrisW67
28th February 2016, 20:13
Have you investigated the viewBoxF() of the loaded SVG?

Songg
28th February 2016, 22:14
Have you investigated the viewBoxF() of the loaded SVG?

No. ViewBox is not what i looking for. For my case they are the same:



void DialogImageProperies::showSVG(QSvgRenderer &rndr,QString sFile){

qd << "defaultSize - " << rndr.defaultSize();
qd << "viewBoxF - " << rndr.viewBoxF();

And console output :


src\dlgImgProp.cpp 52 defaultSize - QSize(1250, 961)
src\dlgImgProp.cpp 53 viewBoxF - QRectF(0,0 1250x961.25)

Songg
29th February 2016, 00:52
Solution (or workaround? doesn't matter)

This is svg begin:


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
id="svg2"
version="1.1"
inkscape:version="0.91 r13725"
sodipodi:docname="hhhhhhhhhhh_slide2.svg"
width="1250"
height="961.25">
<defs
id="defs4" />
<sodipodi:namedview
If manually delete width="1250" and height="961.25" then Qt recalculate real itemsBoundingRect of svg)))

Songg
29th February 2016, 11:37
Solution:
Need to manually delete "width", "height", "viewBox" attributes from <svg> element. Then Qt not found predefined boundaries and calculate them by itself.