Always update pressedPosition when drag was enabled

Since e02293a76d and
2e0c29a4bb was committed,
If a item width more than others,the selectionRect.x() always consist
of currentStartSelection item's rect.center().x(),this will cause
selectionRect size is not right.

Because the code of 2e0c29a4bb is to fix
the new bug introduced by e02293a76d,
we need to use a better way to solve QTBUG-78797.

When itemview enable drag,we need always update pressedPosition because
pressedPosition was used to determine the drag distance, otherwise keep
previous logic.

Fixes: QTBUG-78797
Fixes: QTBUG-81542
Fixes: QTBUG-99512
Pick-to: 6.2 6.3
Change-Id: Ibc5020e35b0eb319e4b5546bdba39ff527c209a6
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
bb10
Zhang Hao 2021-12-31 13:56:14 +08:00
parent ae7799a924
commit bb67b6ff26
1 changed files with 13 additions and 12 deletions

View File

@ -1817,9 +1817,16 @@ void QAbstractItemView::mousePressEvent(QMouseEvent *event)
QItemSelectionModel::SelectionFlags command = selectionCommand(index, event);
d->noSelectionOnMousePress = command == QItemSelectionModel::NoUpdate || !index.isValid();
QPoint offset = d->offset();
d->pressedPosition = d->draggedPosition = pos + offset;
if (!(command & QItemSelectionModel::Current))
d->draggedPosition = pos + offset;
// update the pressed position when drag was enable
if (d->dragEnabled)
d->pressedPosition = d->draggedPosition;
if (!(command & QItemSelectionModel::Current)) {
d->pressedPosition = pos + offset;
d->currentSelectionStartIndex = index;
}
else if (!d->currentSelectionStartIndex.isValid())
d->currentSelectionStartIndex = currentIndex();
@ -1839,7 +1846,7 @@ void QAbstractItemView::mousePressEvent(QMouseEvent *event)
command |= d->ctrlDragSelectionFlag;
}
if ((command & QItemSelectionModel::Current) == 0) {
if (!(command & QItemSelectionModel::Current)) {
setSelection(QRect(pos, QSize(1, 1)), command);
} else {
QRect rect(visualRect(d->currentSelectionStartIndex).center(), pos);
@ -1895,16 +1902,10 @@ void QAbstractItemView::mouseMoveEvent(QMouseEvent *event)
|| edit(index, NoEditTriggers, event))
return;
if (d->selectionMode != SingleSelection) {
// Use the current selection start index if it is valid as this will be based on the
// start of the selection and not the last item being pressed which can be different
// when in extended selection
topLeft = d->currentSelectionStartIndex.isValid()
? visualRect(d->currentSelectionStartIndex).center()
: d->pressedPosition - d->offset();
} else {
if (d->selectionMode != SingleSelection)
topLeft = d->pressedPosition - d->offset();
else
topLeft = bottomRight;
}
d->checkMouseMove(index);