From 6326246f0d1e60287f3220df4cf9cfbc014329e5 Mon Sep 17 00:00:00 2001 From: Viktor Arvidsson Date: Wed, 17 Jan 2024 10:19:01 +0100 Subject: [PATCH] QAbstractItemView: Release tab focus when hidden When hiding a widget that has focus we try to focus the next widget in the focus chain by running focusNextPrevChild. The abstract item view overrides this to step the items but does not account for this hide case which makes focusing not only not work, but also by hiding the widget the selection in the item view gets changed. Pick-to: 6.5 Change-Id: I29d40a1fb86ced60ec742b2753a87383846a89b3 Reviewed-by: Viktor Arvidsson Reviewed-by: Santhosh Kumar Reviewed-by: Volker Hilsheimer (cherry picked from commit d9397479e6dfc2d7b73cab6dcbcda4cccdc20b8a) Reviewed-by: Qt Cherry-pick Bot (cherry picked from commit 92e1bf8e3b42ff24f86210a4e29ea790bff9fabf) --- src/widgets/itemviews/qabstractitemview.cpp | 2 +- .../tst_qabstractitemview.cpp | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/widgets/itemviews/qabstractitemview.cpp b/src/widgets/itemviews/qabstractitemview.cpp index ab189f41507..dcec8e4ae48 100644 --- a/src/widgets/itemviews/qabstractitemview.cpp +++ b/src/widgets/itemviews/qabstractitemview.cpp @@ -1644,7 +1644,7 @@ Qt::TextElideMode QAbstractItemView::textElideMode() const bool QAbstractItemView::focusNextPrevChild(bool next) { Q_D(QAbstractItemView); - if (d->tabKeyNavigation && isEnabled() && d->viewport->isEnabled()) { + if (d->tabKeyNavigation && isVisible() && isEnabled() && d->viewport->isEnabled()) { QKeyEvent event(QEvent::KeyPress, next ? Qt::Key_Tab : Qt::Key_Backtab, Qt::NoModifier); keyPressEvent(&event); if (event.isAccepted()) diff --git a/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp b/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp index b5b0d807768..12a6dd17bc8 100644 --- a/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp +++ b/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp @@ -151,6 +151,7 @@ private slots: void testSpinBoxAsEditor_data(); void testSpinBoxAsEditor(); void removeIndexWhileEditing(); + void focusNextOnHide(); private: static QAbstractItemView *viewFromString(const QByteArray &viewType, QWidget *parent = nullptr) @@ -3548,5 +3549,32 @@ void tst_QAbstractItemView::removeIndexWhileEditing() } } +void tst_QAbstractItemView::focusNextOnHide() +{ + QWidget widget; + QTableWidget table(10, 10); + table.setTabKeyNavigation(true); + QLineEdit lineEdit; + + QHBoxLayout layout; + layout.addWidget(&table); + layout.addWidget(&lineEdit); + widget.setLayout(&layout); + + widget.setTabOrder({&table, &lineEdit}); + + widget.show(); + QVERIFY(QTest::qWaitForWindowExposed(&widget)); + + QVERIFY(table.hasFocus()); + QCOMPARE(table.currentIndex(), table.model()->index(0, 0)); + QTest::keyPress(&table, Qt::Key_Tab); + QCOMPARE(table.currentIndex(), table.model()->index(0, 1)); + + table.hide(); + QCOMPARE(table.currentIndex(), table.model()->index(0, 1)); + QVERIFY(lineEdit.hasFocus()); +} + QTEST_MAIN(tst_QAbstractItemView) #include "tst_qabstractitemview.moc"