user1662454
user1662454

Reputation: 51

Qt, how to make Tooltip visible without hovering over the control?

I want tooltips to be visible by default when the container widget gets focus/visible.

I want tooltip to appear without being mouse hover on the respective control.

Upvotes: 5

Views: 2310

Answers (1)

Pavel Zdenek
Pavel Zdenek

Reputation: 7293

You need to subclass the widget and override handler(s) for event(s) which should produce tooltip display. In the handler, create a QHelpEvent of type QEvent::ToolTip and enqueue it at the event loop. Finally call the parent's original handler, to let it do what was originally intended.

So specifically for getting focus on button, it would be

class MyButton : public QPushButton {
  virtual void focusInEvent(QFocusEvent *) {
    if(evt->gotFocus()) {
      QPoint pos(0,0);
      QHelpEvent* help = new QHelpEvent(
        QEvent::ToolTip,pos,this->mapToGlobal(pos));
      QCoreApplication::postEvent(this,help);
    }
    QPushButton::focusInEvent(evt);
  }
}

For visibility you would override

void QWidget::showEvent(QShowEvent * event);

and do similar code. You need to adjust relative pos to your taste, because originally tooltip dependens on mouse position which you don't have here. Also keep very tight control over making your widgets focused and/or visible. By default, something gets focus all the time, so you will get tooltips all over the place.

Upvotes: 7

Related Questions