Qt 的 Widget 以拥有变为定制的 GUI (图形用户界面) 方式,处理键盘聚焦。
基本问题是,用户击键可以指向屏幕中的任何几个窗口,且打算在窗口内的任何几个 Widget 中。当用户按下键时,他们期望转到正确位置,且软件必须转到满足这种预期。系统必须确定击键指向哪个应用程序,该应用程序中的哪个窗口,及该窗口中的哪个 Widget。
把键盘焦点指向特定 Widget 的自定义演变,是这些:
这些运动机制各不相同,且不同类型的 Widget 只接收它们中某些的聚焦。我们将依次分别介绍它们。
按下 Tab 是到目前为止,使用键盘移动聚焦的最常见方式 (有时,数据录入应用程序中的 Enter 所做的如同 Tab ;可以很轻松达成这,在 Qt 中通过实现 事件过滤器 )。
按下 Tab ,在常用的现今所有窗口系统中,把键盘聚焦移到每个窗口循环列表中的下一 Widget。 Tab 聚焦按某个方向沿循环列表移动, Shift+Tab 按其它方向。次序对于 Tab 按下从 Widget 移到 Widget 称为 Tab 次序。
可以定制选项卡次序使用 QWidget::setTabOrder () (若不, Tab 一般按 Widget 构造次序移动聚焦)。Qt Designer 提供视觉改变 Tab 次序的手段。
由于按下 Tab 如此常见,大多数可以拥有聚焦的 Widget 都应支持 Tab 聚焦。主要例外是不常使用的 Widget,且有一些键盘加速器 (或错误处理程序) 会移动聚焦。
例如,在数据录入对话框,可能有字段只在 1% 的情况下是必要的。在这种对话框中, Tab 可以跳过此字段,且对话框可以使用这些机制之一:
Another exception to Tab support is text-entry widgets that must support the insertion of tabs; almost all text editors fall into this class. Qt treats Ctrl+Tab as Tab and Ctrl+Shift+Tab as Shift+Tab , and such widgets can reimplement QWidget::event () and handle Tab before calling QWidget::event () to get normal processing of all other keys. However, since some systems use Ctrl+Tab for other purposes, and many users aren't aware of Ctrl+Tab anyway, this isn't a complete solution.
This is perhaps even more common than pressing Tab on computers with a mouse or other pointing device.
Clicking to move the focus is slightly more powerful than Tab . While it moves the focus to a widget, for editor widgets it also moves the text cursor (the widget's internal focus) to the spot where the mouse is clicked.
Since it is so common and people are used to it, it's a good idea to support it for most widgets. However, there is also an important reason to avoid it: you may not want to remove focus from the widget where it was.
For example, in a word processor, when the user clicks the 'B' (bold) tool button, what should happen to the keyboard focus? Should it remain where it was, almost certainly in the editing widget, or should it move to the 'B' button?
We advise supporting click-to-focus for widgets that support text entry, and to avoid it for most widgets where a mouse click has a different effect. (For buttons, we also recommend adding a keyboard shortcut: QAbstractButton and its subclasses make this very easy.)
在 Qt 中,仅 QWidget::setFocusPolicy () 函数影响点击聚焦。
It's not unusual for keyboard shortcuts to move the focus. This can happen implicitly by opening modal dialogs, but also explicitly using focus accelerators such as those provided by QLabel::setBuddy (), QGroupBox ,和 QTabBar .
We advise supporting shortcut focus for all widgets that the user may want to jump to. For example, a tab dialog can have keyboard shortcuts for each of its pages, so the user can press e.g. Alt+P to step to the P rinting page. It is easy to overdo this: there are only a few keys, and it's also important to provide keyboard shortcuts for commands. Alt+P is also used for Paste, Play, Print, and Print Here in the 标准加速键 列表,例如。
On Microsoft Windows, mouse wheel usage is always handled by the widget that has keyboard focus. On macOS and X11, it's handled by the widget that gets other mouse events.
The way Qt handles this platform difference is by letting widgets move the keyboard focus when the wheel is used. With the right focus policy on each widget, applications can work idiomatically correctly on Windows, macOS , and X11.
在这种情况下,应用程序必须确定窗口中的哪个 Widget 应接收聚焦。
这可以很简单:若聚焦前已在此窗口中,那么,最后一个拥有聚焦的 Widget 应重新获得聚焦。Qt 会自动做到这。
若聚焦前从未在此窗口中,且知道聚焦应该从哪里开始,调用 QWidget::setFocus () 在应接收聚焦的 Widget 中,先于调用 QWidget::show () 它。若不,Qt 将拾取合适 Widget。