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

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

I want the tooltip to appear without hovering over the corresponding control.

+4
source share
1 answer

You need to subclass the widget and override the handler for events (s) that should display a tooltip. In the handler, create a QHelpEvent type QEvent::ToolTip and run it in the event loop. Finally, call the parent source handler to do what it originally intended.

So specifically for focusing on the button 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 override

 void QWidget::showEvent(QShowEvent * event); 

and execute the same code. You need to adjust the relative pos to your taste, because initially the hint depends on the position of the mouse, which you do not have here. Also keep very tight control so that your widgets are focused and / or visible. By default, something is constantly focused, so you will get tooltips everywhere.

+1
source

Source: https://habr.com/ru/post/1433553/


All Articles