Qt 4 allows developers to write cross-platform applications that are usable by visually impaired users as well as by users with other disabilities. Qt accessibility will make applications accessible to more users and opens the governmental market, where accessibility is often a requirement.
The accessibility classes have been extended in various ways since Qt 3. We added new functions and new enum values, and revised the API to make it more consistent with the rest of Qt. We also added two properties to QWidget , accessibleName and accessibleDescription , that can be set in Qt Designer to provide basic help texts without having to write any code.
Qt's accessibility architecture is as follows. Qt offers one generic interface, QAccessibleInterface ,可用於包裹所有 Widget 和對象 (如 QPushButton )。此單一接口為輔助技術,提供所有必要元數據。Qt 為內置 Widget 作為插件提供此接口的實現。
A more detailed overview of the accessibility support in Qt can be found on the 可訪問性 頁麵。
By default, Qt applications are run with accessibility support enabled on Windows and Mac OS X. On Unix/X11 platforms, applications must be launched in an environment with the
QT_ACCESSIBILITY
variable set to 1. For example, this is set in the following way with the bash shell:
export QT_ACCESSIBILITY=1
Accessibility features are built into Qt by default when the libraries are configured and built.
當開發自定義 Widget 時,可以創建自定義子類為 QAccessibleInterface 並將它們分發作為插件 (使用 QAccessiblePlugin ) 或將它們編譯進應用程序。同樣,Qt 的預定義可訪問性支持可以構建作為插件 (默認),或直接構建進 Qt 庫。使用插件的主要優點是可訪問性類僅加載進內存,若它們被實際使用;它們不會減慢沒有使用輔助技術的常見情況。
除瞭 QAccessibleInterface , Qt includes two convenience classes, QAccessibleObject and QAccessibleWidget , that provide the lowest common denominator of metadata (e.g., widget geometry, window title, basic help text). You can use them as base classes when wrapping your custom QObject or QWidget 子類。
Another new feature in Qt 4 is that Qt can now support other backends in addition to the predefined ones. This is done by subclassing QAccessibleBridge .
The first example illustrates how to provide accessibility information for a custom widget. We can use QAccessibleWidget as a base class and reimplement various functions:
class MyWidgetInterface : public QAccessibleWidget { public: MyWidgetInterface(QWidget *widget, Role role); QString text(Text text, int child) const; State state(int child) const; QString actionText(int action, Text text, int child) const; bool doAction(int action, int child, const QVariantList ¶ms); ... };
Here's how we would implement the doAction() function to call a function named click() on the wrapped MyWidget object when the user invokes the object's default action or "presses" it.
bool MyWidgetInterface::doAction(int action, int child, const QVariantList ¶ms) { if (child || !widget()->isEnabled()) return false; switch (action) { case DefaultAction: case Press: { MyWidget *widget = qobject_cast<MyWidget *>(object()); if (widget) widget->click(); } return true; } return QAccessibleWidget::doAction(action, child, params); }
To export the widget interface as a plugin, we must subclass QAccessibleFactory:
QStringList MyFactory::keys() const { return QStringList() << "MyWidget" << "MyOtherWidget"; } QAccessibleInterface *MyFactory::create(const QString &className, QObject *object) { if (classname == "MyWidget") return new MyWidgetInterface(object); if (classname == "MyOtherWidget") return new MyOtherWidgetInterface(object); return 0; } Q_EXPORT_PLUGIN2(myfactory, MyFactory)