From fd894fd68edf3d67975cda8eb9dda43646887b0d Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Wed, 22 Apr 2020 18:55:13 +0200 Subject: QListWidget/View: Update the selection when moving items within the view MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The code for doing that existed in the QListWidget::dropEvent override, and was only using standard itemview APIs to adjust the selection model. There is no reason why dropping items internally in a QListView should not do the exact same thing. As part of moving the code from QListWidget to QListView, replace QList with QVector, and other minor optimizations. [ChangeLog][QtWidgets][QListView] Moving selected items within a list view by drag'n'drop will maintain the selection of those items. Change-Id: Ie24bec38234839dcb2f0b0ee0302cc59ca101631 Fixes: QTBUG-83084 Pick-to: 5.15 Reviewed-by: Jan Arve Sæther --- src/widgets/itemviews/qlistwidget.cpp | 41 ++--------------------------------- 1 file changed, 2 insertions(+), 39 deletions(-) (limited to 'src/widgets/itemviews/qlistwidget.cpp') diff --git a/src/widgets/itemviews/qlistwidget.cpp b/src/widgets/itemviews/qlistwidget.cpp index 7e360f9be8..2a0a1d683f 100644 --- a/src/widgets/itemviews/qlistwidget.cpp +++ b/src/widgets/itemviews/qlistwidget.cpp @@ -1875,45 +1875,8 @@ bool QListWidget::dropMimeData(int index, const QMimeData *data, Qt::DropAction } /*! \reimp */ -void QListWidget::dropEvent(QDropEvent *event) { - Q_D(QListWidget); - if (event->source() == this && d->movement != Static) { - QListView::dropEvent(event); - return; - } - - if (event->source() == this && (event->dropAction() == Qt::MoveAction || - dragDropMode() == QAbstractItemView::InternalMove)) { - QModelIndex topIndex; - int col = -1; - int row = -1; - if (d->dropOn(event, &row, &col, &topIndex)) { - QList selIndexes = selectedIndexes(); - QList persIndexes; - const int selIndexesCount = selIndexes.count(); - persIndexes.reserve(selIndexesCount); - for (int i = 0; i < selIndexesCount; i++) - persIndexes.append(selIndexes.at(i)); - - if (persIndexes.contains(topIndex)) - return; - std::sort(persIndexes.begin(), persIndexes.end()); // The dropped items will remain in the same visual order. - - QPersistentModelIndex dropRow = model()->index(row, col, topIndex); - - int r = row == -1 ? count() : (dropRow.row() >= 0 ? dropRow.row() : row); - for (int i = 0; i < persIndexes.count(); ++i) { - const QPersistentModelIndex &pIndex = persIndexes.at(i); - d->listModel()->move(pIndex.row(), r); - r = pIndex.row() + 1; // Dropped items are inserted contiguously and in the right order. - } - - event->accept(); - // Don't want QAbstractItemView to delete it because it was "moved" we already did it - event->setDropAction(Qt::CopyAction); - } - } - +void QListWidget::dropEvent(QDropEvent *event) +{ QListView::dropEvent(event); } -- cgit v1.2.3