Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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:

    svgform.h:

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

    svgform.cpp:

    
    #include "ui_svgform.h"
    #include "QPainter"
    #include "QSvgRenderer"
    #include "QDebug"
    #include "QToolTip"
    
    svgform::svgform(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::svgform)
    {
        ui->setupUi(this);
        setMouseTracking(true);
    }
    
    // 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);
    
        svgr.load(svgFile);
        qrect = svgr.boundsOnElement("square2");
        QRect viewbox(0,0,100,100); // this is not working...
        svgr.setViewBox(viewbox);
        svgr.render(&painter);
    
        // end paint
        painter.end();
    }
    
    svgform::~svgform()
    {
        delete ui;
    }
    
    

  • Lifetime Qt Champion

    Hi,

    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