Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QScrollbar on mouse hover

    General and Desktop
    2
    4
    1277
    Loading More Posts
    • 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.
    • Cobra91151
      Cobra91151 last edited by

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

      1 Reply Last reply Reply Quote 0
      • dheerendra
        dheerendra Qt Champions 2022 last edited by

        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

        Cobra91151 1 Reply Last reply Reply Quote 8
        • Cobra91151
          Cobra91151 @dheerendra last edited by Cobra91151

          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 Reply Quote 0
          • Cobra91151
            Cobra91151 last edited by

            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 Reply Quote 0
            • First post
              Last post