Underline QLabel's text “on hover”

无人久伴 提交于 2020-01-02 07:58:30

问题


I want to turn the text green and underline it when the mouse cursor goes over a QLabel, however, it just turns green, it does not get underlined.

  QLabel:hover { color: green; text-decoration: underline;}

What am I doing wrong?

EDIT: Fixed, I used:

void QClickableLabel::enterEvent (QEvent *event)
{
    Q_UNUSED (event);
    setStyleSheet ("QLabel { color: green; text-decoration: underline; }");
}

void QClickableLabel::leaveEvent (QEvent *event)
{
    Q_UNUSED (event);
    setStyleSheet ("QLabel { color: black; }");
}

回答1:


According to Qt documentation (for both Qt 4 and Qt 5), QLabel "Does not support the :hover pseudo-state". Guess it's plain luck that it even changes the color...

To emulate, you could create a QLabel subclass and promote your widget to it. Then implement enterEvent() and leaveEvent() methods, doing necessary changes to the widget, e.g.

void MyLabel::enterEvent(QEvent* event)
{
    QFont f = font();
    f.setUnderline(true);
    setFont(f);
}

void MyLabel::leaveEvent(QEvent* event)
{
    QFont f = font();
    f.setUnderline(false);
    setFont(f);
}



回答2:


You can use the following construction:

QLabel *text= new QLabel("Your text"); text->setStyleSheet("font-weight: bold; color: green; text-decoration: underline");

I'm using this and it's works wonderfully. ;)



来源:https://stackoverflow.com/questions/38946327/underline-qlabels-text-on-hover

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!