Which Qt 6 QAccessibleRole values map to platform accessibility roles for custom widgets?
0 reputation · 06 Dec 2023, 04:32 UTC
0 reputation · 06 Dec 2023, 04:32 UTC
Developers want custom Qt 6 widgets to expose meaningful roles to assistive technologies on Windows, macOS, and Linux.
Qt 6’s QAccessibleInterface allows specifying a QAccessibleRole, but the translation of these values to platform‑specific roles (UI Automation on Windows, NSAccessibility on macOS, AT‑SPI on Linux) is not consistently documented. Custom roles often surface as “GenericControl” or missing names, leading to generic spoken labels.
33275 reputation · 06 Dec 2023, 16:07 UTC
The following QAccessibleRole values are reliably translated by Qt 6 to native platform roles on Windows UI Automation, macOS NSAccessibility, and Linux AT‑SPI. Using any of these roles for a custom widget will expose a meaningful role name to assistive technologies.
These mappings come from the Qt 6 accessibility plugins (QWindowsAccessibility, QMacAccessibility, QUnixAccessibility) where the enum values are hard‑coded to the corresponding native role strings. Roles not listed above fall back to generic roles (UIA GenericControl, AXGroup/AXUnknown, AT‑SPI widget/generic) and often result in assistive technologies announcing "GenericControl" or providing no role name.
When a custom widget inherits from QWidget (or QAccessibleWidget) and you call setAccessibleRole(QAccessible::Role), the Qt accessibility bridge forwards the enum to the platform plugin. The plugin looks up the enum in a static table; if a match exists it emits the native role, otherwise it uses the fallback. Therefore, to guarantee a specific native role you must choose a QAccessibleRole that appears in that table.
setAccessibleRole(QAccessible::Button) (or use QAccessibleWidget::setAccessibleRole).name via setAccessibleName and, if needed, a description via setAccessibleDescription.If you are using a Qt 6 patch release earlier than 6.2, the mapping table may differ slightly; confirming your exact Qt version would change the recommendation to either upgrade or verify the mapping manually.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.