1

I want a text to be displayed persistently on the curser event when the cursor is moving, not depending on the cursor position. I used Qtooltip for this purpose. This is the code to show the text:

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
// ...
}

bool Widget::event (QEvent *ev)
{
    if (event->type() == QEvent::ToolTip) {
         QHelpEvent *helpEvent = static_cast<QHelpEvent *>(ev);
         QToolTip::showText(helpEvent->globalPos(), "Something got it");

         return false;
     }
     return QWidget::event(ev);
}

But when I run this code the text is not displayed consistently and it shows up only sometimes, disappears while moving the cursor, and the whole window flickers.

mahya
  • 51
  • 4
  • But the standard for tooltips is to show it at cursor position. Please edit your question to specify exactly what behaviour you expect. – Baumflaum Aug 12 '21 at 17:17
  • 2
    @Baumflaum I suspect the issue is that a tooltip pops up at the cursor position and remains stationary until it disappears whereas the OP wants the tooltip widget to track mouse movement with the text being constantly updated as widgets are entered/left. Perhaps the OP can clarify. – G.M. Aug 12 '21 at 17:41
  • Please elaborate on what you mean by "display a text consistently". This is absolutely unclear. – HiFile.app - best file manager Aug 12 '21 at 21:18
  • Didn't you try to show a tool tip on other event handlers like mouse move etc.? – vahancho Aug 13 '21 at 07:44
  • I need to show an exact text on the cursor constantly no matter where the cursor position is. I need the text to move with the cursor on the screen. QTooltip method shows the text only in specific positions or when an event happens like a mouse hovering on the widget. But what I need is to show a constant text near the cursor while the cursor is moving on the Qwindow. – mahya Aug 14 '21 at 19:29

1 Answers1

0

You can probably achieve what you want by intercepting mouse move events rather than the tool tip notifications...

class tooltip_event_filter: public QLabel {
  using super = QLabel;
public:
  tooltip_event_filter ()
    {
      setWindowFlags(windowFlags()
                     | Qt::BypassWindowManagerHint
                     | Qt::FramelessWindowHint
        );
    }
protected:
  virtual bool eventFilter (QObject *obj, QEvent *event) override
    {
      if (event->type() == QEvent::MouseMove) {

        /*
         * Note the QPoint(1, 0) offset here.  If we don't do that then the
         * subsequent call to qApp->widgetAt(QCursor::pos()) will return a
         * pointer to this widget itself.
         */
        move(QCursor::pos() + QPoint(1, 0));
        if (const auto *w = qApp->widgetAt(QCursor::pos())) {
          setText(QString("widget@%1").arg((qulonglong)w));
          show();
        } else {
          hide();
        }
      }
      return super::eventFilter(obj, event);
    }
};

Then install an instance of tooltip_event_filter on the application instance...

tooltip_event_filter tooltip_event_filter;
qApp->installEventFilter(&tooltip_event_filter);

The example shown simply displays the address of the widget under the mouse pointer as it's moved.

G.M.
  • 12,232
  • 2
  • 15
  • 18
  • Thanks for the code which works fine but when I install the instance in main.cpp. In this case, the text is shown but widgets like buttons are not working. Also, in case I install the instance in the widget classes, it does not work at all! – mahya Aug 16 '21 at 16:37
  • Not sure I understand that behaviour. I added the code shown to a few of my day to day apps and it seems to behave as expected. If you want an event filter that should be installed on individual widgets then that will look slightly different. It would need to intercept enter and leave events and [mouse tracking](https://doc.qt.io/qt-5/qwidget.html#mouseTracking-prop) would need to be enabled for all widgets of interest. – G.M. Aug 16 '21 at 17:40
  • My widget includes Qpieslices in a QpieSeries. I need to show the slices' values while hovering on them. I need a tooltip to show the value of the current slice that the mouse is hovering on it. Also, as long as the cursor is on a slice, the tooltip should constantly show its value. – mahya Aug 16 '21 at 22:20