From d9397479e6dfc2d7b73cab6dcbcda4cccdc20b8a Mon Sep 17 00:00:00 2001 From: Viktor Arvidsson Date: Wed, 17 Jan 2024 10:19:01 +0100 Subject: 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.7 6.6 6.5 Change-Id: I29d40a1fb86ced60ec742b2753a87383846a89b3 Reviewed-by: Viktor Arvidsson Reviewed-by: Santhosh Kumar Reviewed-by: Volker Hilsheimer --- .../qabstractitemview/tst_qabstractitemview.cpp | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp') diff --git a/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp b/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp index d4ad780fa8..b221047798 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" -- cgit v1.2.3