QScrollbar on mouse hover
Solved
General and Desktop
-
Hello! I want to change width of QScrollbar on mouse hover. Any ideas how to implement this?
-
You can handle mouseMoveEvent and change the size of your scrollBar accordingly.
- Create your CustomScrollBar and handle the mouseTracking & MouseMove.
- Handle mouseEvents in parent object of your scrollBar object and resize accordingly.
- You can use eventFilter also.
-
I want to apply width to all QScrollBars on hover in application. What about setting a stylesheet?
I have tried to use eventFilter:
qApp->installEventFilter(this); bool Test::eventFilter(QObject *object, QEvent *event) { qDebug() << "Event"; if (event->type() == QEvent::Scroll) { QScrollEvent *scrollEvent = static_cast<QScrollEvent*>(event); if (scrollEvent->scrollState() == QScrollEvent::Enter) { this->setStyleSheet("QScrollBar:vertical {width: 20px;}"); } if (scrollEvent->scrollState() == QScrollEvent::Leave) { this->setStyleSheet("QScrollBar:vertical {width: 12px;}"); } } return QObject::eventFilter(object, event); }
Event seems working but not change QScrollBar width on mouse hover. What I am doing wrong? Thanks in advance.
-
I have figured it out and now it works. I post code here, so others can find a solution.
Code:
qApp->installEventFilter(this); bool Test::eventFilter(QObject *object, QEvent *event) { if (event->type() == QEvent::Enter) { qDebug() << "Enter"; this->setStyleSheet("Your style here"); } if (event->type() == QEvent::Leave) { qDebug() << "Leave"; this->setStyleSheet("Your style here"); } return QObject::eventFilter(object, event); }