Alex Pappas
Alex Pappas

Reputation: 2598

QTableView Scrolling by dragging/grabbing the table itself

How do you make a QtableView dragable in terms of scrolling it up and down. Not changing its position. Like when you use a hand tool in a pdf reader to scroll the pages.

Upvotes: 1

Views: 1723

Answers (1)

Alex Pappas
Alex Pappas

Reputation: 2598

The QScroller class enables kinetic scrolling for any scrolling widget or graphics item.

QScroller *scroller = QScroller::scroller(ui->tableView);
ui->tableView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
ui->tableViews->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);

You might want to disable overshoot policy of scroller...

QScrollerProperties properties = QScroller::scroller(scroller)->scrollerProperties();
QVariant overshootPolicy = QVariant::fromValue<QScrollerProperties::OvershootPolicy>(QScrollerProperties::OvershootAlwaysOff);
properties.setScrollMetric(QScrollerProperties::VerticalOvershootPolicy, overshootPolicy);
scroller->setScrollerProperties(properties);
properties.setScrollMetric(QScrollerProperties::HorizontalOvershootPolicy, overshootPolicy);
scroller->setScrollerProperties(properties);
//Scrolling Gesture
scroller->grabGesture(ui->tableView,QScroller::LeftMouseButtonGesture);

You can replace LeftMouseButtonGesture with TouchGesture for touchscreens.

Upvotes: 2

Related Questions