Skip to content

Commit

Permalink
Fix efficiency issue with QAbstractItemView's response to dataChanged().
Browse files Browse the repository at this point in the history
Before, the response to a range of cells was to refresh the entire view.
Now, it creates a rectangle that encloses the top-left and bottom-right
cells.

An equivalent change is already in the Qt 6 code, but Qt has stoppped
accepting changes to Qt 5.

Change-Id: Icbd47034ca2b8cbe134b56a47d2caff9609189a1
  • Loading branch information
ulatekh committed Feb 4, 2022
1 parent 40143c1 commit 5e56947
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/widgets/itemviews/qabstractitemview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3300,11 +3300,15 @@ void QAbstractItemView::scrollToBottom()
*/
void QAbstractItemView::update(const QModelIndex &index)
{
update(index, index);
}
void QAbstractItemView::update(const QModelIndex &topLeft, const QModelIndex &bottomRight)
{
Q_D(QAbstractItemView);
if (index.isValid()) {
const QRect rect = visualRect(index);
//this test is important for peformance reason
if (topLeft.isValid() && bottomRight.isValid()) {
const QRect rect = visualRect(topLeft).united(visualRect(bottomRight));
//this test is important for performance reason
//For example in dataChanged we simply update all the cells without checking
//it can be a major bottleneck to update rects that aren't even part of the viewport
if (d->viewport->rect().intersects(rect))
Expand Down Expand Up @@ -3343,7 +3347,7 @@ void QAbstractItemView::dataChanged(const QModelIndex &topLeft, const QModelInde
} else {
d->updateEditorData(topLeft, bottomRight);
if (isVisible() && !d->delayedPendingLayout)
d->viewport->update();
update(topLeft, bottomRight);
}

#ifndef QT_NO_ACCESSIBILITY
Expand Down
1 change: 1 addition & 0 deletions src/widgets/itemviews/qabstractitemview.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ public Q_SLOTS:
void scrollToTop();
void scrollToBottom();
void update(const QModelIndex &index);
void update(const QModelIndex &topLeft, const QModelIndex &bottomRight);

protected Q_SLOTS:
virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>());
Expand Down

0 comments on commit 5e56947

Please sign in to comment.