QListView scrollbar movement on mouse hover at edges of the listview
-
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.
-
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. -
You need to check global mouse (@QWidget::mapToGlobal@) co-ordinates in addition to checking QListView coordinates.
-
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.