diff options
author | Frank Reininghaus <frank78ac@googlemail.com> | 2015-03-10 23:05:22 +0100 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2015-03-22 14:38:07 +0000 |
commit | f1e90768095be37419ba4bf3c69ec5c860bdbcb6 (patch) | |
tree | ac652951734f49112a6dcc8337b814af14cba03c /src | |
parent | e6b97fd2c793ae702cf2102e43ce9efb4d27983a (diff) |
Fix problems with extended selection after changing the model contents
Storing the position of the first selected item in the view can lead to
wrong extended selections if the contents of the model change. Future
Shift-clicks will always use the previous position of the first selected
item, which may not be correct any more, to calculate the current
selection.
To fix this problem, a QPersistentModelIndex is used to keep track of
the first selected item.
A new unit test is added. Moreover, one function of the QTableView unit
test is changed such that it shows the view prior to performing the
test. Without this change, this test may fail. That the test, which
simulates mouse presses without showing the view, worked at all seems
to be a coincidence, as pointed out in QTBUG-18009.
Task-number: QTBUG-18009
Change-Id: I0d844fbd1a994c279a7c8ee5d9b5b9fccecd25bf
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/widgets/itemviews/qabstractitemview.cpp | 22 | ||||
-rw-r--r-- | src/widgets/itemviews/qabstractitemview_p.h | 1 |
2 files changed, 13 insertions, 10 deletions
diff --git a/src/widgets/itemviews/qabstractitemview.cpp b/src/widgets/itemviews/qabstractitemview.cpp index 6aa2ba08cd..2caf5d1eb3 100644 --- a/src/widgets/itemviews/qabstractitemview.cpp +++ b/src/widgets/itemviews/qabstractitemview.cpp @@ -1055,7 +1055,7 @@ void QAbstractItemView::setCurrentIndex(const QModelIndex &index) d->selectionModel->setCurrentIndex(index, command); d->currentIndexSet = true; if ((command & QItemSelectionModel::Current) == 0) - d->pressedPosition = visualRect(currentIndex()).center() + d->offset(); + d->currentSelectionStartIndex = index; } } @@ -1707,10 +1707,12 @@ void QAbstractItemView::mousePressEvent(QMouseEvent *event) QItemSelectionModel::SelectionFlags command = selectionCommand(index, event); d->noSelectionOnMousePress = command == QItemSelectionModel::NoUpdate || !index.isValid(); QPoint offset = d->offset(); - if ((command & QItemSelectionModel::Current) == 0) + if ((command & QItemSelectionModel::Current) == 0) { d->pressedPosition = pos + offset; - else if (!indexAt(d->pressedPosition - offset).isValid()) - d->pressedPosition = visualRect(currentIndex()).center() + offset; + d->currentSelectionStartIndex = index; + } + else if (!d->currentSelectionStartIndex.isValid()) + d->currentSelectionStartIndex = currentIndex(); if (edit(index, NoEditTriggers, event)) return; @@ -1722,7 +1724,7 @@ void QAbstractItemView::mousePressEvent(QMouseEvent *event) d->autoScroll = false; d->selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate); d->autoScroll = autoScroll; - QRect rect(d->pressedPosition - offset, pos); + QRect rect(visualRect(d->currentSelectionStartIndex).center(), pos); if (command.testFlag(QItemSelectionModel::Toggle)) { command &= ~QItemSelectionModel::Toggle; d->ctrlDragSelectionFlag = d->selectionModel->isSelected(index) ? QItemSelectionModel::Deselect : QItemSelectionModel::Select; @@ -2319,16 +2321,16 @@ void QAbstractItemView::keyPressEvent(QKeyEvent *event) // note that we don't check if the new current index is enabled because moveCursor() makes sure it is if (command & QItemSelectionModel::Current) { d->selectionModel->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate); - if (!indexAt(d->pressedPosition - d->offset()).isValid()) - d->pressedPosition = visualRect(oldCurrent).center() + d->offset(); - QRect rect(d->pressedPosition - d->offset(), visualRect(newCurrent).center()); + if (!d->currentSelectionStartIndex.isValid()) + d->currentSelectionStartIndex = oldCurrent; + QRect rect(visualRect(d->currentSelectionStartIndex).center(), visualRect(newCurrent).center()); setSelection(rect, command); } else { d->selectionModel->setCurrentIndex(newCurrent, command); - d->pressedPosition = visualRect(newCurrent).center() + d->offset(); + d->currentSelectionStartIndex = newCurrent; if (newCurrent.isValid()) { // We copy the same behaviour as for mousePressEvent(). - QRect rect(d->pressedPosition - d->offset(), QSize(1, 1)); + QRect rect(visualRect(newCurrent).center(), QSize(1, 1)); setSelection(rect, command); } } diff --git a/src/widgets/itemviews/qabstractitemview_p.h b/src/widgets/itemviews/qabstractitemview_p.h index c5753a91ff..b792228312 100644 --- a/src/widgets/itemviews/qabstractitemview_p.h +++ b/src/widgets/itemviews/qabstractitemview_p.h @@ -370,6 +370,7 @@ public: QPersistentModelIndex enteredIndex; QPersistentModelIndex pressedIndex; + QPersistentModelIndex currentSelectionStartIndex; Qt::KeyboardModifiers pressedModifiers; QPoint pressedPosition; bool pressedAlreadySelected; |