Important: Please read the Qt Code of Conduct -

QSvgRenderer::boundsOnElement() does not work with svg zoom in/out using viewbox?

  • I am working on a project where i have to display a QToolTips on certain svg Elements inside a svg image. This works as it should, until i start using a svg viewbox and am zooming in or out. Because the QRectF that is returned does not grow or shrink when zooming in or out. It always stays on the original position using the original height/width. So as a result the QRectF that is returned by QSvgRenderer::boundOnElements(), is causing the tooltips to being displayed on a totally different location then it should. Is there a way to use QSvgRenderer::boundsOnElement() using the viewbox? The documentation isn't clear about this. And i am starting to wonder if boundsOnElement can handle this at all... But no one seems to be able to tell me if i am doing something wrong, or if the function just cant handle a viewbox...

    working original width/height svg image, returns correct QRectF

    not working zoomed in svg image, using viewbox. And as you can see, no tooltip...

    My demonstration code:


    #ifndef SVGFORM_H
    #define SVGFORM_H
    #include <QWidget>
    #include <QMouseEvent>
    namespace Ui {
    class svgform;
    class svgform : public QWidget
        explicit svgform(QWidget *parent = nullptr);
        void paintEvent(QPaintEvent *event);
        void mouseMoveEvent(QMouseEvent *event);
        Ui::svgform *ui;
        QRectF qrect;
    #endif // SVGFORM_H


    #include "ui_svgform.h"
    #include "QPainter"
    #include "QSvgRenderer"
    #include "QDebug"
    #include "QToolTip"
    svgform::svgform(QWidget *parent) :
        ui(new Ui::svgform)
    // Implement in your widget
    void svgform::mouseMoveEvent(QMouseEvent *event){
        if(qrect.contains(event->pos())) {
            QToolTip::showText(event->globalPos(), "test", nullptr);
    void svgform::paintEvent(QPaintEvent * /* event */)
        //Create qpainter object
        QPainter painter(this);
        // file location main wheel in qstring
        QString svgFile = "/home/arjan/qtprojects/svgtooltip/svgTooltip/square.svg";
        // create svg object
        QSvgRenderer svgr(svgFile);
        qrect = svgr.boundsOnElement("square2");
        QRect viewbox(0,0,100,100); // this is not working...
        // end paint
        delete ui;

  • Lifetime Qt Champion


    I might be wrong but the rectangle returned by that method has no knowledge at all with regard to the widget where it is painted on.

Log in to reply