QTableView: prevent QTableView from hanging when removing rows.
authorPierre Rossi <pierre.rossi@nokia.com>
Mon, 4 Apr 2011 13:58:42 +0000 (15:58 +0200)
committerOlivier Goffart <olivier.goffart@nokia.com>
Tue, 10 May 2011 10:54:44 +0000 (12:54 +0200)
The problem was introduced in cd2afafb where we removed some code that
was meant to adjust the header's offset upon row removal.
The problem with this is that visualIndexAt() is likely to return -1 in
QHeaderView::paintEvent, which in turn will lead to calling paintSection
for each and every section.

Task-number: QTBUG-18551
Reviewed-by: Thierry
(cherry picked from commit d814e378987348ce2123d083b01ea6fb6c3e6bbf)

src/gui/itemviews/qtableview.cpp
src/gui/itemviews/qtableview.h

index e494ee5..59a3d15 100644 (file)
@@ -1104,6 +1104,21 @@ void QTableView::setRootIndex(const QModelIndex &index)
 /*!
   \reimp
 */
+void QTableView::doItemsLayout()
+{
+    Q_D(QTableView);
+    QAbstractItemView::doItemsLayout();
+    if (verticalScrollMode() == QAbstractItemView::ScrollPerItem)
+        d->verticalHeader->setOffsetToSectionPosition(verticalScrollBar()->value());
+    else
+        d->verticalHeader->setOffset(verticalScrollBar()->value());
+    if (!d->verticalHeader->updatesEnabled())
+        d->verticalHeader->setUpdatesEnabled(true);
+}
+
+/*!
+  \reimp
+*/
 void QTableView::setSelectionModel(QItemSelectionModel *selectionModel)
 {
     Q_D(QTableView);
@@ -1975,9 +1990,13 @@ QModelIndexList QTableView::selectedIndexes() const
     previous number of rows is specified by \a oldCount, and the new
     number of rows is specified by \a newCount.
 */
-void QTableView::rowCountChanged(int /*oldCount*/, int /*newCount*/ )
+void QTableView::rowCountChanged(int oldCount, int newCount )
 {
     Q_D(QTableView);
+    //when removing rows, we need to disable updates for the header until the geometries have been
+    //updated and the offset has been adjusted, or we risk calling paintSection for all the sections
+    if (newCount < oldCount)
+        d->verticalHeader->setUpdatesEnabled(false);
     d->doDelayedItemsLayout();
 }
 
index d4be086..7ab9d08 100644 (file)
@@ -71,6 +71,7 @@ public:
     void setModel(QAbstractItemModel *model);
     void setRootIndex(const QModelIndex &index);
     void setSelectionModel(QItemSelectionModel *selectionModel);
+    void doItemsLayout();
 
     QHeaderView *horizontalHeader() const;
     QHeaderView *verticalHeader() const;