QScrollbar on mouse hover



  • 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.

    1. Create your CustomScrollBar and handle the mouseTracking & MouseMove.
    2. Handle mouseEvents in parent object of your scrollBar object and resize accordingly.
    3. 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);
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.