From dcdf76ac693455191c8b1c480a0956c22f5ac28e Mon Sep 17 00:00:00 2001 From: Michael Weghorn Date: Sat, 4 Nov 2023 22:55:03 +0100 Subject: [PATCH] a11y: Only send focus event when item has focus When the current item in an item view has changed, only send an accessible focus event when the item view actually has focus. Sending a focus event when the item doesn't actually have focus is incorrect and breaks focus tracking in assistive technology, like the Orca screen reader. With this change in place, the focus event for the newly selected item/child still gets sent when changing the item using the keyboard or by clicking on it using the mouse (since the item view has focus in those cases), but not when it gets changed programmatically while the keyboard focus is somewhere else. Fixes: QTBUG-118800 Pick-to: 6.5 Change-Id: I5540cdfce6d0ed076d4bb871e5862fddcdf05941 Reviewed-by: Volker Hilsheimer (cherry picked from commit a149b3fd533525d0b260893019b41b300a554f8d) Reviewed-by: Qt Cherry-pick Bot (cherry picked from commit 9cd1b8a28d4f24e9f25ce071421e2b61c96de1f5) --- src/widgets/itemviews/qlistview.cpp | 2 +- src/widgets/itemviews/qtableview.cpp | 2 +- src/widgets/itemviews/qtreeview.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/widgets/itemviews/qlistview.cpp b/src/widgets/itemviews/qlistview.cpp index a4cb4c76fbc..5835af36a87 100644 --- a/src/widgets/itemviews/qlistview.cpp +++ b/src/widgets/itemviews/qlistview.cpp @@ -3408,7 +3408,7 @@ void QListView::currentChanged(const QModelIndex ¤t, const QModelIndex &pr QAbstractItemView::currentChanged(current, previous); #if QT_CONFIG(accessibility) if (QAccessible::isActive()) { - if (current.isValid()) { + if (current.isValid() && hasFocus()) { int entry = visualIndex(current); QAccessibleEvent event(this, QAccessible::Focus); event.setChild(entry); diff --git a/src/widgets/itemviews/qtableview.cpp b/src/widgets/itemviews/qtableview.cpp index 9d6499de5b9..8e0db77ae28 100644 --- a/src/widgets/itemviews/qtableview.cpp +++ b/src/widgets/itemviews/qtableview.cpp @@ -3478,7 +3478,7 @@ void QTableView::currentChanged(const QModelIndex ¤t, const QModelIndex &p { #if QT_CONFIG(accessibility) if (QAccessible::isActive()) { - if (current.isValid()) { + if (current.isValid() && hasFocus()) { Q_D(QTableView); int entry = d->accessibleTable2Index(current); QAccessibleEvent event(this, QAccessible::Focus); diff --git a/src/widgets/itemviews/qtreeview.cpp b/src/widgets/itemviews/qtreeview.cpp index 7454fb83a7c..1ee0cc71ee4 100644 --- a/src/widgets/itemviews/qtreeview.cpp +++ b/src/widgets/itemviews/qtreeview.cpp @@ -4036,7 +4036,7 @@ void QTreeView::currentChanged(const QModelIndex ¤t, const QModelIndex &pr viewport()->update(d->visualRect(current, QTreeViewPrivate::FullRow)); } #if QT_CONFIG(accessibility) - if (QAccessible::isActive() && current.isValid()) { + if (QAccessible::isActive() && current.isValid() && hasFocus()) { Q_D(QTreeView); QAccessibleEvent event(this, QAccessible::Focus);