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. QListView scrollbar movement on mouse hover at edges of the listview

QListView scrollbar movement on mouse hover at edges of the listview

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.5k Views 1 Watching
  • 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.
  • T Offline
    T Offline
    tesmai4
    wrote on last edited by
    #1

    I am displaying thumbnails in QListView from my model. Scrollbar is visible in the listview as number of items is more than the size of the listview.

    I want to move my scroll bar left or right when user hovers mouse of left or right edges of the listview. mousetracking for listview is already enabled.

    Any help is appreciated.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dagan9528
      wrote on last edited by
      #2

      Hi ,
      I have the same problem. Did your find any work round?
      I have already check the scrollbar example, but it can not movement on mouse hover at the listview.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tesmai4
        wrote on last edited by
        #3

        You need to check global mouse (@QWidget::mapToGlobal@) co-ordinates in addition to checking QListView coordinates.

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          try something like this:

          @
          class MyListView : public QListView
          {
          public:
          MyListView(QWidget* parent = 0);

          virtual bool eventFilter(QObject*, QEvent*);
          

          protected slots:
          void onTimerout();

          protected:
          QTimer m_Timer;
          enum { LeftScrollDirection, RightScrollDirection, NoScrollDirection } m_ScrollDirection;
          }
          @

          @
          MyListView::MyListView(QWidget* parent)
          : QListView(parent), m_ScrollDirection(NoScrollDirection)
          {
          this->viewport()->setMouseTracking(true);
          this->viewport()->installEventFilter(this);

           m_Timer.setInterval(300);
           m_Timer.setSingleShot(false);
           connect( &m_Timer, SIGNAL(timeout()), this, SLOT(onTimeout()) );
          

          }

          bool MyListView::eventFilter(QObject* obj, QEvent* event)
          {
          if( obj == this->viewport() )
          {
          switch( event->type() )
          {
          case QEvent::MouseMove:
          {
          QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
          //check pos and set the scroll direction direction member accordingly
          int x = mouseEvent->pos().x();
          int viewportRight = this->viewport()->rect().width();

                               if( 0<= x && x <= 50 )
                               {
                                     m_ScrollDirection = LeftScrollDirection;
                                     if( ! m_Timer.isActive() ) m_Timer.start();
                               }
                               else if( (viewportRight-50) <= x  && x <= viewportRight )
                               {
                                     m_ScrollDirection = LeftScrollDirection;
                                     if( ! m_Timer.isActive() ) m_Timer.start();
                               }
                               else
                               {
                                     m_ScrollDirection = NoScrollDirection;
                                     if( m_Timer.isActive() ) m_Timer.stop();
                               }
                       }
                       break;
                       case QEvent::Leave:
                       {
                               if( m_Timer.isActive() )
                                    m_Timer.stop();
                       }
                       break;
                 }
            }
          
            return QListView::eventFilter(obj,event);
          

          }

          void MyList::onTimeout()
          {
          QScrollBar* sb = this->horizontalScrollBar();

              switch( m_ScrollDirection )
              {
                    case LeftScrollDirection:
                            sb->setValue( sb->value() - 10 );
                    case RightScrollDirection
                            sb->setValue( sb->value() + 10 );
                    break;
                    default:
                    case NoScrollDirection:
                    break;
              }
          

          }
          @

          i have written this code straight from mind and thus haven't tested it. So forgive errors. It should give you an idea though.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          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