Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
focus.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2021 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page qtquick-input-focus.html
6
\title Keyboard Focus in Qt Quick
7
\brief handling keyboard focus.
8
9
When a key is pressed or released, a key event is generated and delivered to the
10
focused Qt Quick \l Item. To facilitate the construction of reusable components
11
and to address some of the cases unique to fluid user interfaces, the Qt Quick items add a
12
scope based extension to Qt's traditional keyboard focus model.
13
14
\section1 Key Handling Overview
15
16
When the user presses or releases a key, the following occurs:
17
\list 1
18
\li Qt receives the key action and generates a key event.
19
\li If a \l QQuickWindow is the \l{QGuiApplication::focusWindow()}{focus window}
20
of the application, the key event is delivered to it.
21
\li The key event is delivered by the scene to the \l Item with
22
\e {active focus}. If no item has active focus, the key event is ignored.
23
\li If the \l QQuickItem with active focus accepts the key event, propagation
24
stops. Otherwise the event is sent to the Item's parent until
25
the event is accepted, or the root item is reached.
26
27
If the \c {Rectangle} type in the following example has active focus and the \c A key is pressed,
28
the event will not be propagated further. Upon pressing the \c B key, the event will propagate to the root
29
item and thus be ignored.
30
31
\snippet qml/focus/rectangle.qml simple key event
32
\snippet qml/focus/rectangle.qml simple key event end
33
34
\li If the root \l Item is reached, the key event is \l {QEvent::ignore()}{ignored} and regular Qt key handling continues.
35
36
\endlist
37
38
See also the \l {Keys}{Keys attached property} and \l {KeyNavigation}{KeyNavigation attached property}.
39
40
\section1 Querying the Active Focus Item
41
42
Whether or not an \l Item has active focus can be queried through the
43
\c {Item::activeFocus} property. For example, here we have a \l Text
44
type whose text is determined by whether or not it has active focus.
45
46
\snippet qml/focus/rectangle.qml active focus
47
48
More than one item can have active focus at the same time: the item that
49
receives key events, and every \l FocusScope that contains it. For more
50
information, see \l {Acquiring Focus and Focus Scopes}.
51
52
To find the single item that receives key events, use the
53
\l [attached] {Window::activeFocusItem}{Window.activeFocusItem} attached
54
property, or QQuickWindow::activeFocusItem() from C++. For example, a focus
55
scope can check whether it has active focus itself, rather than through one
56
of its children:
57
58
\qml
59
FocusScope {
60
id: scope
61
readonly property bool hasOwnActiveFocus: Window.activeFocusItem === scope
62
}
63
\endqml
64
65
\section1 Acquiring Focus and Focus Scopes
66
67
An \l Item requests focus by setting the \c focus property to \c true.
68
69
For very simple cases simply setting the \c focus property is sometimes
70
sufficient. If we run the following example with the
71
\l {Prototyping with the QML Runtime Tool}{qml tool},
72
we see that the \c {keyHandler} type has active focus and
73
pressing the \c A, \c B, or \c C keys modifies the text appropriately.
74
75
\snippet qml/focus/basicwidget.qml focus true
76
77
\image declarative-qmlfocus1.png {Blue rounded widget showing Key C was pressed}
78
79
However, were the above example to be used as a reusable or imported component,
80
this simple use of the \c focus property is no longer sufficient.
81
82
To demonstrate, we create two instances of our previously defined component and
83
set the first one to have focus. The intention is that when the \c A, \c B, or
84
\c C keys are pressed, the first of the two components receives the event and
85
responds accordingly.
86
87
The code that imports and creates two MyWidget instances:
88
\snippet qml/focus/widget.qml window
89
90
The MyWidget code:
91
\snippet qml/focus/MyWidget.qml mywidget
92
93
We want the first \c MyWidget object to have the focus, so we set its
94
\c focus property to \c true. However, by running the code, it can happen that
95
the second widget receives the focus.
96
97
\image declarative-qmlfocus2.png
98
{Two stacked widgets with second green one showing Key A was pressed}
99
100
Looking at both \c MyWidget and \c window code, the problem is evident - there
101
are three types that set the \c focus property to \c true. The two
102
\c {MyWidget}s set the \c focus to \c true and the \c window component also sets the
103
focus. Ultimately, only one type can have keyboard focus, and the system has
104
to decide which type receives the focus. Since QML does not guarantee which element
105
will have its properties initialized first, it might be that the last \c MyWidget
106
gets the initial focus.
107
108
This problem is due to visibility. The \c MyWidget component would like to have
109
the focus, but it cannot control the focus when it is imported or reused.
110
Likewise, the \c window component does not have the ability to know if its
111
imported components are requesting the focus.
112
113
To solve this problem, QML introduces a concept known as a \e {focus scope}.
114
For existing Qt users, a focus scope is like an automatic focus proxy.
115
A focus scope is created by declaring the \l FocusScope type.
116
117
In the next example, a \l FocusScope type is added to the component, and the
118
visual result shown.
119
120
\snippet qml/focus/myfocusscopewidget.qml widget in focusscope
121
122
\image declarative-qmlfocus3.png
123
{Two stacked widgets with first blue one showing Key B was pressed}
124
125
126
Conceptually \e {focus scopes} are quite simple.
127
\list
128
\li Within each focus scope one object may have \c {Item::focus} set to
129
\c true. If more than one \l Item has the \c focus property set, the
130
last type to set the \c focus will have the focus and the others are unset,
131
similar to when there are no focus scopes.
132
\li When a focus scope receives active focus, the contained type with
133
\c focus set (if any) also gets the active focus. If this type is
134
also a \l FocusScope, the proxying behavior continues. Both the
135
focus scope and the sub-focused item will have the \c activeFocus property set.
136
\endlist
137
138
Note that, since the FocusScope type is not a visual type, the properties
139
of its children need to be exposed to the parent item of the FocusScope. Layouts
140
and positioning types will use these visual and styling properties to create
141
the layout. In our example, the \c Column type cannot display the two widgets
142
properly because the FocusScope lacks visual properties of its own. The MyWidget
143
component directly binds to the \c rectangle properties to allow the \c Column
144
type to create the layout containing the children of the FocusScope.
145
146
So far, the example has the second component statically selected. It is trivial
147
now to extend this component to make it clickable, and add it to the original
148
application. We still set one of the widgets as focused by default.
149
Now, clicking either MyClickableWidget gives it focus and the other widget
150
loses the focus.
151
152
The code that imports and creates two MyClickableWidget instances:
153
\snippet qml/focus/clickablewidget.qml clickable window
154
155
The MyClickableWidget code:
156
\snippet qml/focus/MyClickableWidget.qml clickable in focusscope
157
158
\image declarative-qmlfocus4.png
159
{Two clickable widgets each displaying their last pressed key}
160
161
When a QML \l Item explicitly relinquishes focus (by setting its
162
\c focus property to \c false while it has active focus), the
163
system does not automatically select another type to receive focus. That is,
164
it is possible for there to be no currently active focus.
165
166
See \l{Qt Quick Examples - Key Interaction} for a
167
demonstration of moving keyboard focus between multiple areas using FocusScope
168
types.
169
170
\section1 Advanced Uses of Focus Scopes
171
172
Focus scopes allow focus to allocation to be easily partitioned. Several
173
QML items use it to this effect.
174
175
\l ListView, for example, is itself a focus scope. Generally this isn't
176
noticeable as \l ListView doesn't usually have manually added visual children.
177
By being a focus scope, \l ListView can focus the current list item without
178
worrying about how that will effect the rest of the application. This allows the
179
current item delegate to react to key presses.
180
181
This contrived example shows how this works. Pressing the \c Return key will
182
print the name of the current list item.
183
184
\snippet qml/focus/advancedFocus.qml FocusScope delegate
185
186
\image declarative-qmlfocus5.png {List with three editable names: Bob, John, Michael}
187
188
While the example is simple, there is a lot going on behind the scenes. Whenever
189
the current item changes, the \l ListView sets the delegate's \c {Item::focus}
190
property. As the \l ListView is a focus scope, this doesn't affect the
191
rest of the application. However, if the \l ListView itself has
192
active focus this causes the delegate itself to receive active focus.
193
In this example, the root type of the delegate is also a focus scope,
194
which in turn gives active focus to the \l {TextInput} type that actually performs
195
the work of handling the \c {Return} key.
196
197
All of the QML view classes, such as \l PathView and \l GridView, behave
198
in a similar manner to allow key handling in their respective delegates.
199
*/
qtdeclarative
src
quick
doc
src
concepts
input
focus.qdoc
Generated on
for Qt by
1.18.0