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 <volker.hilsheimer@qt.io>
(cherry picked from commit a149b3fd533525d0b260893019b41b300a554f8d)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 9cd1b8a28d4f24e9f25ce071421e2b61c96de1f5)
This commit is contained in:
Michael Weghorn 2023-11-04 22:55:03 +01:00 committed by Qt Cherry-pick Bot
parent 4d8b7b584a
commit dcdf76ac69
3 changed files with 3 additions and 3 deletions

View File

@ -3408,7 +3408,7 @@ void QListView::currentChanged(const QModelIndex &current, const QModelIndex &pr
QAbstractItemView::currentChanged(current, previous); QAbstractItemView::currentChanged(current, previous);
#if QT_CONFIG(accessibility) #if QT_CONFIG(accessibility)
if (QAccessible::isActive()) { if (QAccessible::isActive()) {
if (current.isValid()) { if (current.isValid() && hasFocus()) {
int entry = visualIndex(current); int entry = visualIndex(current);
QAccessibleEvent event(this, QAccessible::Focus); QAccessibleEvent event(this, QAccessible::Focus);
event.setChild(entry); event.setChild(entry);

View File

@ -3478,7 +3478,7 @@ void QTableView::currentChanged(const QModelIndex &current, const QModelIndex &p
{ {
#if QT_CONFIG(accessibility) #if QT_CONFIG(accessibility)
if (QAccessible::isActive()) { if (QAccessible::isActive()) {
if (current.isValid()) { if (current.isValid() && hasFocus()) {
Q_D(QTableView); Q_D(QTableView);
int entry = d->accessibleTable2Index(current); int entry = d->accessibleTable2Index(current);
QAccessibleEvent event(this, QAccessible::Focus); QAccessibleEvent event(this, QAccessible::Focus);

View File

@ -4036,7 +4036,7 @@ void QTreeView::currentChanged(const QModelIndex &current, const QModelIndex &pr
viewport()->update(d->visualRect(current, QTreeViewPrivate::FullRow)); viewport()->update(d->visualRect(current, QTreeViewPrivate::FullRow));
} }
#if QT_CONFIG(accessibility) #if QT_CONFIG(accessibility)
if (QAccessible::isActive() && current.isValid()) { if (QAccessible::isActive() && current.isValid() && hasFocus()) {
Q_D(QTreeView); Q_D(QTreeView);
QAccessibleEvent event(this, QAccessible::Focus); QAccessibleEvent event(this, QAccessible::Focus);