简体   繁体   English

如何在Qt的子小部件中忽略父小部件的工具提示?

[英]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. 我有一个QWidget,它已经使用setTooltip设置了工具提示,并且在该窗口小部件中,我有一个子窗口小部件。

The problem is that the child widget doesn't have a tooltip specified (ie "") 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 谢谢,艾伦

As vahancho said, an event filter should do what you want: 正如vahancho所说,事件过滤器应该执行您想要的操作:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM