Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QToolTip::showText doesn't work in GraphicsView click handler
Qt 6.12 LTS is out! See what's new in the release blog

QToolTip::showText doesn't work in GraphicsView click handler

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.1k 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
    huderlem
    wrote on last edited by huderlem
    #1

    I want to show a tooltip whenever the user clicks on a QGraphicsView widget. To expose a click handler, I have created the following subclass of QGraphicsView called ClickableGraphicsView :

    class ClickableGraphicsView : public QGraphicsView
    {
        Q_OBJECT
    public:
        ClickableGraphicsView() : QGraphicsView() {}
        ClickableGraphicsView(QWidget *parent) : QGraphicsView(parent) {}
    
    public:
        void mousePressEvent(QMouseEvent *event) {
            QGraphicsView::mousePressEvent(event);
            emit this->clicked(event);
        }
        void mouseDoubleClickEvent(QMouseEvent *event) {
            QGraphicsView::mouseDoubleClickEvent(event);
            emit this->clicked(event);
        }
    
    signals:
        void clicked(QMouseEvent *event);
    };
    

    This seems to work great--I can successfully hook into the clicked signal. However, the tooltip does not appear anywhere.

    QObject::connect(myClickableGraphicsView, &ClickableGraphicsView::clicked, [=](){
        // This is called successfully, logic runs fine.
        DoStuff();
    
        // This doesn't work!
        QToolTip::showText(QPoint(0, 0), "This doesn't work...");
    });
    

    When I setup the exact same code, but for a QPushButton, the tooltip does show up in the top-left corner, as expected.

    QObject::connect(myPushButton, &QPushButton::clicked, [=](){
        QToolTip::showText(QPoint(0, 0), "This works!");
    }
    

    I tried some other techniques, like emitting a signal from these click handlers, but the same issue persists. I'm a bit out of ideas now. Is there something inherent about a QGraphicsView's event-handling system that would get in the way of the tooltip? Thanks!

    1 Reply Last reply
    0
    • H Offline
      H Offline
      huderlem
      wrote on last edited by
      #2

      Bumping this, since I haven't received a response, yet.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Can you provide a minimal compilable example ?
        By the way, which Qt version and OS are you using ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi
          Make sure you emit Clicked in Release event not pressEvent as that seems to not work as expected.

          
          #include <QMouseEvent>
          #include <qgraphicsview.h>
          #include <qgraphicsscene.h>
          #include <QToolTip>
          #include <QGraphicsView>
          
          class MyGraphicsView : public QGraphicsView
          {
              Q_OBJECT
          public:
              MyGraphicsView(QWidget *parent = nullptr) : QGraphicsView(parent) { }
              void mouseReleaseEvent(QMouseEvent *pMouseEvent) override
              {
                  QGraphicsView::mouseReleaseEvent(pMouseEvent);
                  emit clicked();
              }
          signals:
              void clicked();
          };
          
          -- usage ---
            QObject::connect(  ui->graphicsView, &MyGraphicsView::clicked, [this]() {
                  QToolTip::showText( ui->graphicsView->mapToGlobal(QPoint(0, 0)),
                                      "This doesn't work...");
              });
          
          note the mapToGlobal
          

          result
          alt text

          H 1 Reply Last reply
          1
          • mrjjM mrjj

            Hi
            Make sure you emit Clicked in Release event not pressEvent as that seems to not work as expected.

            
            #include <QMouseEvent>
            #include <qgraphicsview.h>
            #include <qgraphicsscene.h>
            #include <QToolTip>
            #include <QGraphicsView>
            
            class MyGraphicsView : public QGraphicsView
            {
                Q_OBJECT
            public:
                MyGraphicsView(QWidget *parent = nullptr) : QGraphicsView(parent) { }
                void mouseReleaseEvent(QMouseEvent *pMouseEvent) override
                {
                    QGraphicsView::mouseReleaseEvent(pMouseEvent);
                    emit clicked();
                }
            signals:
                void clicked();
            };
            
            -- usage ---
              QObject::connect(  ui->graphicsView, &MyGraphicsView::clicked, [this]() {
                    QToolTip::showText( ui->graphicsView->mapToGlobal(QPoint(0, 0)),
                                        "This doesn't work...");
                });
            
            note the mapToGlobal
            

            result
            alt text

            H Offline
            H Offline
            huderlem
            wrote on last edited by
            #5

            @mrjj Thanks--this works perfectly and resolves my issue.

            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
            • Unsolved