summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/widgets/itemviews/qabstractitemview.cpp6
-rw-r--r--tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp32
2 files changed, 37 insertions, 1 deletions
diff --git a/src/widgets/itemviews/qabstractitemview.cpp b/src/widgets/itemviews/qabstractitemview.cpp
index 1619635dbf..179e542ccc 100644
--- a/src/widgets/itemviews/qabstractitemview.cpp
+++ b/src/widgets/itemviews/qabstractitemview.cpp
@@ -4093,8 +4093,12 @@ QItemSelectionModel::SelectionFlags QAbstractItemView::selectionCommand(const QM
if (d->pressedAlreadySelected)
return QItemSelectionModel::NoUpdate;
break;
- case QEvent::KeyPress:
case QEvent::MouseButtonRelease:
+ // clicking into area with no items does nothing
+ if (!index.isValid())
+ return QItemSelectionModel::NoUpdate;
+ Q_FALLTHROUGH();
+ case QEvent::KeyPress:
// ctrl-release on selected item deselects
if ((keyModifiers & Qt::ControlModifier) && d->selectionModel->isSelected(index))
return QItemSelectionModel::Deselect | d->selectionBehaviorFlags();
diff --git a/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp b/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp
index 63b0446088..e81f5c6787 100644
--- a/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp
+++ b/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp
@@ -141,6 +141,7 @@ private slots:
void selectionCommand();
void mouseSelection_data();
void mouseSelection();
+ void keepSingleSelectionOnEmptyAreaClick();
void scrollerSmoothScroll();
void inputMethodOpensEditor_data();
void inputMethodOpensEditor();
@@ -3078,6 +3079,37 @@ void tst_QAbstractItemView::mouseSelection()
}
/*!
+ Make sure that when clicking on empty space in the view, we don't
+ unselect the current row.
+ QTBUG-105870
+*/
+void tst_QAbstractItemView::keepSingleSelectionOnEmptyAreaClick()
+{
+ QListWidget view;
+ view.setSelectionMode(QAbstractItemView::SingleSelection);
+ QListWidgetItem *lastItem;
+ for (int i = 0; i < 5; i++)
+ lastItem = new QListWidgetItem("item " + QString::number(i), &view);
+
+ // Make widget large enough so that there is empty area below the last item
+ view.setFixedSize(300, 500);
+ view.show();
+ QVERIFY(QTest::qWaitForWindowActive(&view));
+
+ // Select third row
+ view.setCurrentRow(2);
+
+ // Click below the last row
+ QPoint targetPoint = view.visualItemRect(lastItem).bottomLeft();
+ targetPoint += QPoint(10, 10);
+
+ QTest::mouseClick(view.viewport(), Qt::MouseButton::LeftButton, Qt::NoModifier, targetPoint);
+
+ QCOMPARE(view.currentRow(), 2);
+ QVERIFY(view.currentItem()->isSelected());
+}
+
+/*!
Verify that scrolling an autoScroll enabled itemview with a QScroller
produces a continuous, smooth scroll without any jumping around due to
the currentItem negotiation between QAbstractItemView and QScroller.