3

Currently, if the user clicks on a cell that is only partially visible, the window automatically scrolls over so that the cell is fully displayed. Is there any way to stop the table doing this? Thanks

alexisdm
  • 29,448
  • 6
  • 64
  • 99
cindyln
  • 33
  • 1
  • 3

2 Answers2

12

You can easily disable this behavior with:

ui->tableWidget->setAutoScroll(false);

alexisdm's answer is dealing with another problem. Suppose you are incrementally appending new rows to your table and you want to maintain current vertical scroll position. I am dealing with this second problem and alexisdm's answer seems promising.

Community
  • 1
  • 1
Ali
  • 452
  • 5
  • 15
  • This also works on QTableView tables. Overriding TableWidget::scrollTo() is apparently just an unnecessary, extra step. – CodeLurker Aug 22 '23 at 19:02
3

The scrolling is done by QAbstractItemView which call the virtual function scrollTo with index the hint EnsureVisible. You can't prevent the call, because it is done through a private timer, but you can change what the scrollTo function does:

void TableWidget::scrollTo(const QModelIndex &index, ScrollHint hint)
{
    if(hint == QAbstractItemView::EnsureVisible)
        return;
    QTableWidget::scrollTo(index, hint);
}

And to still be able to scroll to an item manually, you could write another member function that would call QTableWidget::scrollTo.

alexisdm
  • 29,448
  • 6
  • 64
  • 99