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.6 6.5
Change-Id: I29d40a1fb86ced60ec742b2753a87383846a89b3
Reviewed-by: Viktor Arvidsson <viktor.arvidss@gmail.com>
Reviewed-by: Santhosh Kumar <santhosh.kumar.selvaraj@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit d9397479e6dfc2d7b73cab6dcbcda4cccdc20b8a)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Viktor Arvidsson 2024-01-17 10:19:01 +01:00 committed by Qt Cherry-pick Bot
parent 08868b1c4b
commit 92e1bf8e3b
2 changed files with 29 additions and 1 deletions

View File

@ -1661,7 +1661,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())

View File

@ -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"