简体   繁体   English

在Qt中使用setWhatIsThis和QPushButton

[英]Using setWhatIsThis in Qt with QPushButton

I need to show a "help text" when the mouse is over a pushbutton, I googled and I tried some code but in vain. 当鼠标悬停在按钮上时,我需要显示“帮助文本”,我用Google搜索并尝试了一些代码但是徒劳无功。

Can anyone correct it to me? 任何人都可以纠正它吗?

if (event->type() == QEvent::Enter)
{
    if (obj == q1)
    {
        iarm->printStatus("hi"); // For debugging
        QAction *newAct = new QAction(tr("&New"), this);
        newAct->setShortcut(tr("Ctrl+N"));
        newAct->setStatusTip(tr("Create a new file"));
        newAct->setWhatsThis(tr("Click this option to create a new file."));
    }
}

Instead of going via the QAction, just use your QPushButton's (well, QWidget's really) setToolTip(const QString &) . 而不是通过QAction,只需使用你的QPushButton(好吧,QWidget真的) setToolTip(const QString &)

That will set a tooltip for your button/widget which appears when your mouse hovers over it. 这将为您的按钮/小部件设置工具提示,当鼠标悬停在按钮/小部件上时会显示该工具提示。 It seems that is what you're going for, rather than the "What's This" message you're trying to use now. 这似乎是你想要的,而不是你现在试图使用的“这是什么”的消息。

You can use QpushButton's property setToolTip ( const QString & ) for the same. 您可以使用QpushButton的属性setToolTip(const QString&)。 This will display your help text on mouse hovering over Qpushbutton. 这将显示鼠标悬停在Qpushbutton上的帮助文本。

For Smart text as per your requirement you can set stylesheet for Tooltip:- 对于智能文本,您可以根据自己的要求设置工具提示的样式表: -

QToolTip { color: #fff; QToolTip {color:#fff; background-color: #000; 背景色:#000; border: none; border:none; } }

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

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