Important: Please read the Qt Code of Conduct -

QGraphicsSvgItem control size

  • Question
    What's the proper way of setting a "scale offset" or "resize offset" when working with QGraphicsSvgItem to control how big the rendered SVG is in scene coordinates? My source SVG has a size/viewbox of 0, 0, 24, 24 and therefore the rendered SVG is only 24x24 in size - but I'd like it to be larger.
    I started dicking around with retrieving the QSvgRenderer of the QGraphicsSvgItem using QGraphicsSvgItem::renderer() and then using QSvgRenderer::setViewBox() but that didn't lead me anywhere near what I am after as that just changes the viewbox of the resulted rendering.

    Basically I am looking for something such as QGraphicsSvgItem::setScaleOffset() or QSvgRenderer::setSize().

    Currently I have a custom class named node that inherits from QGraphicsItem. Essentially it paints itself as a rectangle with rounded corners, drop shadow and an icon (in the top left corner of the item's rectangle).

    So far I had a QIcon member in my node class and inside of node::paint() I use QIcon::paint() to paint the icon. This works great both in theory and practice.
    The icon I am painting is an SVG image. But due to the current way of painting a QIcon it looks bad at any zoom level other than 100% as the rastered icon is being scaled when the user manipulates the zoom value.
    To solve this issue, I removed my QIcon member and added a QGraphicsSvgItem child item to my node class.
    I managed to get everything working (as in: The SVG is successfully being rendered as part of my node class). Also the scaling works as expected: I can zoom in an out and the icon now always looks great.

    However, my issue is that the source SVG I have has a default view port of 0, 0, 24, 24. The resulting icon is too small in reference to my node rectangle. Therefore, I set out to find a way of how to manually add a scale factor to my SVG so that the rendered icon is larger relative to the node::boundingRect() while also keeping the already working zoom scaling stuff.

    Unfortunately, I couldn't find a way of how to do this.

  • Lifetime Qt Champion


    I don't know either but what about nuking the viewport before loading the file ?

  • @SGaist said in QGraphicsSvgItem control size:

    I don't know either but what about nuking the viewport before loading the file ?

    Could you elaborate a bit more what your suggestion consists of?

  • Lifetime Qt Champion

    SVG being XML, you could modify the view port as you see fit before loading it. But I may have misunderstood the issue at hand.

  • Lifetime Qt Champion

    If you open the SVG in say inkscape and change its page size (ViewBox) does it then
    work as expected in your app ?

    Im not sure i understand what the goal since QSvgRenderer::setViewBox() did change the rendered size for me with QIcon so are we looking for something else ?

Log in to reply