QAbstractItemView: use QPMI in currentChanged in case of layoutChanged

If the model's setData() emits layoutChanged, it invalidates QModelIndexes
that this code is working with, so we have to use a persistent model
index for 'current' to protect against that. For 'previous', Volker
Hilsheimer had the idea of moving code around so we're done with it
before calling commitData().

Fixes: QTBUG-127852
Change-Id: Ieb6ebc9603e7a753d78aab11ce81ad87d71f5084
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
(cherry picked from commit cbc5b4fbe12b6c4410bf482cd1caac933c8b63ea)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
David Faure 2024-09-10 13:04:53 +02:00 committed by Qt Cherry-pick Bot
parent d53c3809de
commit c37256f6e4

View File

@ -3803,38 +3803,43 @@ void QAbstractItemView::currentChanged(const QModelIndex &current, const QModelI
Q_D(QAbstractItemView); Q_D(QAbstractItemView);
Q_ASSERT(d->model); Q_ASSERT(d->model);
QPersistentModelIndex persistentCurrent(current); // in case commitData() moves things around (QTBUG-127852)
if (previous.isValid()) { if (previous.isValid()) {
QModelIndex buddy = d->model->buddy(previous); QModelIndex buddy = d->model->buddy(previous);
QWidget *editor = d->editorForIndex(buddy).widget.data(); QWidget *editor = d->editorForIndex(buddy).widget.data();
if (isVisible()) {
update(previous);
}
if (editor && !d->persistent.contains(editor)) { if (editor && !d->persistent.contains(editor)) {
commitData(editor); const bool rowChanged = current.row() != previous.row();
if (current.row() != previous.row()) commitData(editor); // might invalidate previous, don't use after this line (QTBUG-127852)
if (rowChanged)
closeEditor(editor, QAbstractItemDelegate::SubmitModelCache); closeEditor(editor, QAbstractItemDelegate::SubmitModelCache);
else else
closeEditor(editor, QAbstractItemDelegate::NoHint); closeEditor(editor, QAbstractItemDelegate::NoHint);
} }
if (isVisible()) {
update(previous);
}
} }
QItemSelectionModel::SelectionFlags command = selectionCommand(current, nullptr); const QModelIndex newCurrent = persistentCurrent;
if ((command & QItemSelectionModel::Current) == 0)
d->currentSelectionStartIndex = current;
if (current.isValid() && !d->autoScrollTimer.isActive()) { QItemSelectionModel::SelectionFlags command = selectionCommand(newCurrent, nullptr);
if ((command & QItemSelectionModel::Current) == 0)
d->currentSelectionStartIndex = newCurrent;
if (newCurrent.isValid() && !d->autoScrollTimer.isActive()) {
if (isVisible()) { if (isVisible()) {
if (d->autoScroll) if (d->autoScroll)
scrollTo(current); scrollTo(newCurrent);
update(current); update(newCurrent);
edit(current, CurrentChanged, nullptr); edit(newCurrent, CurrentChanged, nullptr);
if (current.row() == (d->model->rowCount(d->root) - 1)) if (newCurrent.row() == (d->model->rowCount(d->root) - 1))
d->fetchMore(); d->fetchMore();
} else { } else {
d->shouldScrollToCurrentOnShow = d->autoScroll; d->shouldScrollToCurrentOnShow = d->autoScroll;
} }
} }
setAttribute(Qt::WA_InputMethodEnabled, (current.isValid() && (current.flags() & Qt::ItemIsEditable))); setAttribute(Qt::WA_InputMethodEnabled, (newCurrent.isValid() && (newCurrent.flags() & Qt::ItemIsEditable)));
} }
#if QT_CONFIG(draganddrop) #if QT_CONFIG(draganddrop)