From e057f97773c537ee52b2cc609c61d067d7ac8957 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Wed, 17 Apr 2013 20:26:55 +0200 Subject: [PATCH] ExtendedTableWidget: Fix jumpy scrolling Fix wrong scrolling behaviour that ocurred when going to the very bottom of the table and scrolling up slowly. --- src/ExtendedTableWidget.cpp | 19 ++++++++++++------- src/ExtendedTableWidget.h | 1 + 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/ExtendedTableWidget.cpp b/src/ExtendedTableWidget.cpp index 29426793..9337e8b3 100644 --- a/src/ExtendedTableWidget.cpp +++ b/src/ExtendedTableWidget.cpp @@ -69,7 +69,7 @@ void ExtendedTableWidget::updateGeometries() // If so and if it is a SqliteTableModel and if the parent implementation of this method decided that a scrollbar is needed, update its maximum value SqliteTableModel* m = qobject_cast(model()); if(m && verticalScrollBar()->maximum()) - verticalScrollBar()->setMaximum(m->totalRowCount()); + verticalScrollBar()->setMaximum(m->totalRowCount() - numVisibleRows() + 1); } } @@ -79,12 +79,17 @@ void ExtendedTableWidget::vscrollbarChanged(int value) if(!model()) return; - // How many rows are visible right now? - int row_top = rowAt(0) == -1 ? 0 : rowAt(0); - int row_bottom = rowAt(height()) == -1 ? model()->rowCount() : rowAt(height()); - int num_visible_rows = row_bottom - row_top; - // Fetch more data from the DB if necessary - if((value + num_visible_rows) >= model()->rowCount() && model()->canFetchMore(QModelIndex())) + if((value + numVisibleRows()) >= model()->rowCount() && model()->canFetchMore(QModelIndex())) model()->fetchMore(QModelIndex()); } + +int ExtendedTableWidget::numVisibleRows() +{ + // Get the row numbers of the rows currently visible at the top and the bottom of the widget + int row_top = rowAt(0) == -1 ? 0 : rowAt(0); + int row_bottom = rowAt(height()) == -1 ? model()->rowCount() : rowAt(height()); + + // Calculate the number of visible rows + return row_bottom - row_top; +} diff --git a/src/ExtendedTableWidget.h b/src/ExtendedTableWidget.h index 8cec83d8..16a03539 100644 --- a/src/ExtendedTableWidget.h +++ b/src/ExtendedTableWidget.h @@ -12,6 +12,7 @@ public: private: void copy(); + int numVisibleRows(); private slots: void vscrollbarChanged(int value);