QToolTip::showText doesn't work in GraphicsView click handler
-
I want to show a tooltip whenever the user clicks on a
QGraphicsViewwidget. To expose a click handler, I have created the following subclass ofQGraphicsViewcalledClickableGraphicsView: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
clickedsignal. 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! -
Hi,
Can you provide a minimal compilable example ?
By the way, which Qt version and OS are you using ? -
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 mapToGlobalresult

-
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 mapToGlobalresult
