a11y: Notify of name change when setting window title used as a11y name

When no explicit accessible name is set for a window,
QAccessibleWidget::text uses the window title instead for a
non-minimized window.

Send a QAccessible::NameChanged event when changing the
window title results in a change of the accessible name,
to ensure that the AT-SPI cache on Linux gets updated.

Extend tst_QAccessibility::mainWindowTest() to cover the scenario as well.

Note: The entire test function is skipped on platforms not supporting
window activation, e.g. Wayland.

Fixes: QTBUG-124192
Change-Id: I0fa7f683fb5969d6ba9878f6a506c4f192069799
Reviewed-by: Jan Arve Sæther <jan-arve.saether@qt.io>
This commit is contained in:
Michael Weghorn 2024-04-08 14:13:09 +02:00
parent 423ae2de96
commit 25ab8ada10
2 changed files with 21 additions and 0 deletions

View File

@ -6107,6 +6107,13 @@ void QWidget::setWindowTitle(const QString &title)
if (QWidget::windowTitle() == title && !title.isEmpty() && !title.isNull()) if (QWidget::windowTitle() == title && !title.isEmpty() && !title.isNull())
return; return;
#if QT_CONFIG(accessibility)
QString oldAccessibleName;
const QAccessibleInterface *accessible = QAccessible::queryAccessibleInterface(this);
if (accessible)
oldAccessibleName = accessible->text(QAccessible::Name);
#endif
Q_D(QWidget); Q_D(QWidget);
d->topData()->caption = title; d->topData()->caption = title;
d->setWindowTitle_helper(title); d->setWindowTitle_helper(title);
@ -6115,6 +6122,13 @@ void QWidget::setWindowTitle(const QString &title)
QCoreApplication::sendEvent(this, &e); QCoreApplication::sendEvent(this, &e);
emit windowTitleChanged(title); emit windowTitleChanged(title);
#if QT_CONFIG(accessibility)
if (accessible && accessible->text(QAccessible::Name) != oldAccessibleName) {
QAccessibleEvent event(this, QAccessible::NameChanged);
QAccessible::updateAccessibility(&event);
}
#endif
} }

View File

@ -966,6 +966,13 @@ void tst_QAccessibility::mainWindowTest()
QCOMPARE(iface->text(QAccessible::Name), name); QCOMPARE(iface->text(QAccessible::Name), name);
QCOMPARE(iface->role(), QAccessible::Window); QCOMPARE(iface->role(), QAccessible::Window);
QVERIFY(iface->state().active); QVERIFY(iface->state().active);
QTestAccessibility::clearEvents();
QLatin1String newName = QLatin1String("Main window with updated title");
mw->setWindowTitle(newName);
QCOMPARE(iface->text(QAccessible::Name), QLatin1String(newName));
QAccessibleEvent event(mw, QAccessible::NameChanged);
QVERIFY(QTestAccessibility::containsEvent(&event));
} }
QTestAccessibility::clearEvents(); QTestAccessibility::clearEvents();