From 25ab8ada1019adf9e88addd47acb0924831edc5a Mon Sep 17 00:00:00 2001 From: Michael Weghorn Date: Mon, 8 Apr 2024 14:13:09 +0200 Subject: [PATCH] a11y: Notify of name change when setting window title used as a11y name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/widgets/kernel/qwidget.cpp | 14 ++++++++++++++ .../other/qaccessibility/tst_qaccessibility.cpp | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/src/widgets/kernel/qwidget.cpp b/src/widgets/kernel/qwidget.cpp index ead633cda3e..750b2946766 100644 --- a/src/widgets/kernel/qwidget.cpp +++ b/src/widgets/kernel/qwidget.cpp @@ -6107,6 +6107,13 @@ void QWidget::setWindowTitle(const QString &title) if (QWidget::windowTitle() == title && !title.isEmpty() && !title.isNull()) return; +#if QT_CONFIG(accessibility) + QString oldAccessibleName; + const QAccessibleInterface *accessible = QAccessible::queryAccessibleInterface(this); + if (accessible) + oldAccessibleName = accessible->text(QAccessible::Name); +#endif + Q_D(QWidget); d->topData()->caption = title; d->setWindowTitle_helper(title); @@ -6115,6 +6122,13 @@ void QWidget::setWindowTitle(const QString &title) QCoreApplication::sendEvent(this, &e); emit windowTitleChanged(title); + +#if QT_CONFIG(accessibility) + if (accessible && accessible->text(QAccessible::Name) != oldAccessibleName) { + QAccessibleEvent event(this, QAccessible::NameChanged); + QAccessible::updateAccessibility(&event); + } +#endif } diff --git a/tests/auto/other/qaccessibility/tst_qaccessibility.cpp b/tests/auto/other/qaccessibility/tst_qaccessibility.cpp index 5fd695e2e66..cab88e2fc0f 100644 --- a/tests/auto/other/qaccessibility/tst_qaccessibility.cpp +++ b/tests/auto/other/qaccessibility/tst_qaccessibility.cpp @@ -966,6 +966,13 @@ void tst_QAccessibility::mainWindowTest() QCOMPARE(iface->text(QAccessible::Name), name); QCOMPARE(iface->role(), QAccessible::Window); 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();