Search code examples
c++qtqt5qrubberband

Is there a way to show tool tips for QRubberBand?


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.


Solution

  • 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.