Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is there a way to display a QToolTip on top of a svg image displayed with QSvgRenderer?
Forum Updated to NodeBB v4.3 + New Features

Is there a way to display a QToolTip on top of a svg image displayed with QSvgRenderer?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 912 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hermes22
    wrote on last edited by
    #3

    @Christian-Ehrlicher said in Is there a way to display a QToolTip on top of a svg image displayed with QSvgRenderer?:

    You're creating a pointer to a QPoint for no good reason. QToolTip::showText() wants a const reference.

    That's the part i do not yet understand. How can i use the QRectF coardinates in the QToolTip::showtext();

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @Hermes22 said in Is there a way to display a QToolTip on top of a svg image displayed with QSvgRenderer?:

      How can i use the QRectF coardinates in the QToolTip::showtext();

      Pass a const ref, not a pointer (and don't create a QPoint on the heap at all)

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • H Offline
        H Offline
        Hermes22
        wrote on last edited by
        #5

        Oke, I'm getting somewhere now. I finally see a tooltip, but right now its shown where ever the mouse hovers the svg image, instead when hovering a red square i have drawn using svg. So how do i manage to show the tooltip on the location of QRectF and not all over the svg image.

        #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 "svgform.h"
        #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){
        
            QRect rect = qrect.toRect();
            QToolTip::showText(event->pos(), "test", this, rect,100000);
        
        }
        
        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");
        
            // Render paint object
            svgr.render(&painter);
        
            // end paint
            painter.end();
        }
        
        svgform::~svgform()
        {
            delete ui;
        }
        
        
        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #6

          You have to find out where the red square is and only call setToolTip() there by checking if the current mouse position is over the square.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • H Offline
            H Offline
            Hermes22
            wrote on last edited by Hermes22
            #7

            Isn't that what svgr.boundonelements("square") is supposed to do...? Giving me the rectangle of the location of the svg element. I pass the rect data here into the tooltip:

            QRect rect = qrect.toRect(); //Convert QRectF to QRect.
            QToolTip::showText(event->pos(), "test", this, rect,100000);
            

            And how do i check if mouse is where the QRect is? Perhaps this could be the part i am missing?

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #8

              @Hermes22 said in Is there a way to display a QToolTip on top of a svg image displayed with QSvgRenderer?:

              I pass the rect data here into the tooltip:

              No, you don't pass a rect to the tooltip. It's a point where the tooltip should be shown: https://doc.qt.io/qt-5/qtooltip.html#showText

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              2
              • H Offline
                H Offline
                Hermes22
                wrote on last edited by Hermes22
                #9

                After some more puzzling, i found a part of the solution and i see a tooltip now but the tooltip appears only on half of the square. I can't make a screenshot, because it dissapears if i do. Anyway, now i have a new problem. It seems the geometry is smaller then the geometrie of the actual svg image. So this leads me to another question, why is the boundOnElements returning coardinates that are not correct? Can it be that the viewbox of the svg file is a part of the problem?

                void displaynatalchart::mouseMoveEvent(QMouseEvent *event){
                    qDebug() << event->pos();
                
                    qDebug() <<"qrect"<< rect;
                
                    if(event->pos().x() > (rect.x())
                       && event->pos().y() > (rect.y())
                       && event->pos().x() < (rect.x() + rect.width())
                       && event->pos().y() < (rect.y() + (rect.height())) ) {
                            QToolTip::showText(event->pos(), "test", nullptr, rect.toRect(),1000);
                    }
                }
                
                
                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  @Hermes22 said in Is there a way to display a QToolTip on top of a svg image displayed with QSvgRenderer?:

                  if(event->pos().x() > (rect.x())
                     && event->pos().y() > (rect.y())
                     && event->pos().x() < (rect.x() + rect.width())
                     && event->pos().y() < (rect.y() + (rect.height())) )
                  

                  Why that complicated? https://doc.qt.io/qt-5/qrect.html#contains

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  2
                  • H Offline
                    H Offline
                    Hermes22
                    wrote on last edited by Hermes22
                    #11

                    @Christian-Ehrlicher said in Is there a way to display a QToolTip on top of a svg image displayed with QSvgRenderer?:

                    Why that complicated? https://doc.qt.io/qt-5/qrect.html#contains

                    Thanx, probably because i have a talent for thinking to complicated.. Nice solution! Now i need to figure out, why the QRectF returned by the boundOnElement("square") seem not have the correct geometry.

                    mrjjM 1 Reply Last reply
                    0
                    • H Hermes22

                      @Christian-Ehrlicher said in Is there a way to display a QToolTip on top of a svg image displayed with QSvgRenderer?:

                      Why that complicated? https://doc.qt.io/qt-5/qrect.html#contains

                      Thanx, probably because i have a talent for thinking to complicated.. Nice solution! Now i need to figure out, why the QRectF returned by the boundOnElement("square") seem not have the correct geometry.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

                      Hi

                      • Now i need to figure out, why the QRectF returned by the boundOnElement("square") seem not have the correct geometry.

                      I would open the SVG in inkscape and check the viewbox (page)
                      to make sure the red part is centered how you expect it.

                      1 Reply Last reply
                      2
                      • H Offline
                        H Offline
                        Hermes22
                        wrote on last edited by
                        #13

                        Thanx folks, nailed it! Seems that a viewbox can't be used or i'm missing something. Anyway i can work around that!

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved