2

I want to change QScrollBar style on mouse hover. I have tried to get it working by adding eventFilter, but it doesn't work.

Code:

qApp->installEventFilter(this);

bool Test::eventFilter(QObject *object, QEvent *event)
{
    if (event->type() == QEvent::Scroll) {
        QScrollEvent *scrollEvent = static_cast<QScrollEvent*>(event);
        if (scrollEvent->scrollState() == QScrollEvent::Enter) {
            qDebug() << "Enter";
            this->setStyleSheet("QScrollBar:vertical {width: 20px;}");
        }

        if (scrollEvent->scrollState() == QScrollEvent::Leave) {
            qDebug() << "Leave";
            this->setStyleSheet("QScrollBar:vertical {width: 12px;}");
        }
    }

    return QObject::eventFilter(object, event);
}

How can I do this?

MrEricSir
  • 8,044
  • 4
  • 30
  • 35
Cobra91151
  • 610
  • 4
  • 14
  • http://stackoverflow.com/questions/23659134/qt-widen-qscrollbar-when-hover-using-stylesheet – Edward Feb 15 '17 at 08:29

1 Answers1

2

The correct events to handle in your event filter would actually be QEvent::Enter, and QEvent::Leave. QScrollEvent is used when scrolling actually occurs, that's why it was not triggered.

You can also probably directly use stylesheets with the :hover attribute.

Adrien Leravat
  • 2,731
  • 18
  • 32