Mattia F.
Mattia F.

Reputation: 1740

Underline QLabel's text "on hover"

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

Upvotes: 4

Views: 9771

Answers (2)

Campos33
Campos33

Reputation: 11

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

Upvotes: 1

mike.dld
mike.dld

Reputation: 3049

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

Upvotes: 2

Related Questions