Cobra91151
Cobra91151

Reputation: 650

QScrollBar on mouse hover

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?

Upvotes: 2

Views: 533

Answers (1)

Adrien Leravat
Adrien Leravat

Reputation: 2789

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.

Upvotes: 2

Related Questions