Home > Software design >  Is there a way to show tool tips for QRubberBand?
Is there a way to show tool tips for QRubberBand?

Time:06-23

I am trying to set a tool tip for QRubberBand. This is how the constructor of the parent looks. Please note the parent is not a window, but rather a widget of a window.

    roiRB = new QRubberBand( QRubberBand::Rectangle,this);
    roiRB->setGeometry(QRect(QPoint(1,1), QPoint(100,100)));

    roiRB->setAttribute(Qt::WA_AlwaysShowToolTips); //I tried this line as mentioned in the Documentation to all parent classes of the QRubberBand.
    roiRB->setToolTip("Hello");
    roiRB->setToolTipDuration(1000);

However the tooltip is not popping, I tried different values for toolTipDuration as well.

CodePudding user response:

If you look at the documentation, you can read:

Note that by default tooltips are only shown for widgets that are children of the active window. You can change this behavior by setting the attribute Qt::WA_AlwaysShowToolTips on the window, not on the widget with the tooltip.

emphasis mine

You have set the attribute Qt::WA_AlwaysShowToolTips on the widget with the tooltip and not on the window itself which is exactly the opposite of what is mentioned in the documentation.

CodePudding user response:

On digging the source code of QRubberBand, I found out that in the constructor of QRubberBand, the attribute (Qt::WA_TransparentForMouseEvents); is set to true. Manually setting this back to false fixed the issue.

From what I see, tooltips work on MouseEvent calls of the widget, if the this attribute is set to true, the widget ignores MouseEvents and thereby nullifying the setToolTip("abcd").

Therefore,

roiRB = new QRubberBand( QRubberBand::Rectangle,this);
roiRB->setAttribute(Qt::WA_TransparentForMouseEvents,0);

is all you need to do. Do let me know if I have missed something.

  • Related