Render SVG to QPixmap with unknow target size and without loss of quality



  • Hi,

    I have got an *.svg file and i would like to scale it into a QPixmap which has got an unknow target size.
    So when i would like to scale the *.svg file, i do not know the target size of it.

    I have used it so far:

    ...
    QPixmap(":/path/to/svgfile.svg").scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ...
    

    But I realized that it is not appropriate.
    Now the QPixmap is not pixelised, but it has got an other behavior if it appears on a responsive surface.
    Because the smooth transformated 100×100 sized image would be rescaled into a bigger surface,.
    And we are again at the beginning, the image is pixelised.

    How can i reach the next?

    • render QPixmap from *.svg file when the target size is known

    Thanks for advance,
    Peter



  • Hi! Can't you just use QSvgWidget Class?



  • Hi @Wieland,

    Thanks for reply and sorry for my late reply.
    Unfortunately QSvgWidget class can not use in this case.

    Any other solution about it?



  • Hi @pvt-peter ,

    If you can not use QSvgWidget as suggested by @Wieland ,and If you are not obliged to use QPixmap

    You can do something like this:

    Have QWidget named imageContainerWidget;

    Change stylesheet to imageContainerWidget as follows:

    QWidget#imageContainerWidget
    {
    	   image: url(":/images/path/to/svgfile.svg");
    }
    

    And that's it !

    When your imageContainerWidget is resized your image will be resized too,

    The result will be then something like this:

    Before resizing:

    alt text

    After resizing:

    alt text

    Hope this can help!



  • @mostefa: Thanks for your detailed reply.
    It is a good solution.



  • @pvt.peter said in Render SVG to QPixmap with unknow target size and without loss of quality:

    @mostefa: Thanks for your detailed reply.
    It is a good solution.

    You are welcome =)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.