3

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; }");
}
Mattia F.
  • 1,720
  • 11
  • 21

2 Answers2

2

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);
}
mike.dld
  • 2,929
  • 20
  • 21
1

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

Campos33
  • 11
  • 1