Search code examples
c++qttooltipqwidget

How can I ignore parent widget's tooltip in child widget in Qt?


I have a QWidget that a tooltip has been set on using setTooltip and within that widget, I have a child widget.

The problem is that the child widget doesn't have a tooltip specified (i.e. "") but the tooltip of the parent widget is shown. If I do specify a non-blank tooltip in the child widget then it is shown instead of the parent widget's tooltip.

How do I supress this behaviour and have no tooltip shown in the child?

Thanks, Alan


Solution

  • As vahancho said, an event filter should do what you want:

    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {
        setToolTip("This is a parent tooltip");
    
        child = new QWidget(this);
        child->installEventFilter(this);
    }
    
    bool Widget::eventFilter(QObject *obj, QEvent *event)
    {
        if (obj == child && event->type() == QEvent::ToolTip)
        {
            QToolTip::hideText();// this hides the parent's tooltip if it is shown
            return true;// this filters the tooltip event out of processing
        }
    
        return QWidget::eventFilter(obj, event);
    }