[Qt-components] Key navigation

Alberto Mardegan mardy at users.sourceforge.net
Sun Jan 6 16:54:13 CET 2013


On 01/02/2013 11:37 AM, Alberto Mardegan wrote:
> It would be nice if there was a default key navigation flow, implicitly
> built according to the placement of the components inside a window; then
> the keyNavigation properties could be used to override this when needed.
> The QtWidgets library also provides a default key navigation, so it
> seems sensible if the QML components have the same.

Here is a possible implementation:

   https://codereview.qt-project.org/#change,44029

It introduces a FocusChain object which can be used like this:

==================
FocusChain {
   id: focusChain
   rootItem: widgetContainer
}

Grid {
   id: widgetContainer
   ...grid properties...

   Keys.onTabPressed: focusChain.focusNext(Qt.Key_Tab)
   Keys.onBacktabPressed: focusChain.focusNext(Qt.Key_Backtab)

   Button { ... }
   ComboBox { ... }
   ...other widgets...
}
==================

This requires that all focusable widgets (Button, ComboBox, etc.) define 
a "canFocus" property and set it to true.
It works well for simple layouts. If the layout is more complex, one can 
anyway set the Keys.onTabPressed property on a single widget, and that 
will have precedence over the container (in fact, the container won't 
receive the key event at all).

Comments (especially about the API) are very welcome!

Ciao,
   Alberto



More information about the Qt-components mailing list