Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QScrollbar on mouse hover
QtWS25 Last Chance

QScrollbar on mouse hover

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on last edited by
    #1

    Hello! I want to change width of QScrollbar on mouse hover. Any ideas how to implement this?

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      Cobra91151C 1 Reply Last reply
      8
      • dheerendraD dheerendra

        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.
        Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on last edited by Cobra91151
        #3

        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.

        1 Reply Last reply
        0
        • Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on last edited by
          #4

          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);
          }
          
          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved