Documente Academic
Documente Profesional
Documente Cultură
http://qt.nokia.com
1/31
User Interaction
2/31
Objectives
Knowledge of ways to receive user input mouse/touch input keyboard input Awareness of different mechanisms to process input signal handlers property bindings Basic understanding of states and transitions changing the current state using state conditions
User Interaction
3/31
User Interaction
Mouse Input
4/31
User Interaction
Mouse Input
5/31
Mouse Areas
Mouse areas dene parts of the screen where cursor input occurs
Placed and resized like ordinary items using anchors if necessary Two ways to monitor mouse input: handle signals dynamic property bindings
See QML MouseArea Element Reference Documentation
User Interaction
Mouse Input
6/31
User Interaction
Mouse Input
7/31
Dene a Text element Dene a MouseArea child element Use an anchor to apply the area to the entire parent
User Interaction
Mouse Input
8/31
Dene responses to signals with onPressed and onReleased by default, only left clicks are handled set the acceptedButtons property to change this These change the color of the parent Text element Can do something similar with properties...
User Interaction
Mouse Input
9/31
User Interaction
Mouse Input
10/31
Ensure that the mouse area lls the parents area Set the hoverEnabled property
User Interaction
Mouse Input
11/31
Give the MouseArea an id property Refer to this mouse area as mouse_area Enables access to the mouse areas properties
User Interaction
Mouse Input
12/31
color is updated when mouse_area.containsMouse changes Uses a simple JavaScript test "green" if true "white" if false
User Interaction
Mouse Input
13/31
User Interaction
Mouse Input
14/31
User Interaction
Mouse Input
15/31
User Interaction
Keyboard Input
16/31
Keyboard Input
Basic keyboard input is handled in two different use cases:
Accepting text input TextInput and TextEdit Navigation between elements changing the focused element directional (arrow keys), tab and backtab
User Interaction
Keyboard Input
17/31
Assigning Focus
UIs with just one TextInput focus assigned automatically More than one TextInput need to change focus by clicking What happens if a TextInput has no text? no way to click on it unless it has a width or uses anchors Set the focus property to assign focus
User Interaction
Keyboard Input
18/31
Using TextInputs
import QtQuick 1.0 Rectangle { width: 200; height: 112; color: "lightblue" TextInput { anchors.left: parent.left; y: 16 anchors.right: parent.right text: "Field 1"; font.pixelSize: 32 color: focus ? "black" : "gray" focus: true } TextInput { anchors.left: parent.left; y: 64 anchors.right: parent.right text: "Field 2"; font.pixelSize: 32 color: focus ? "black" : "gray" } }
User Interaction
Keyboard Input
19/31
Set the focus property Use anchors to ensure there is something to click on
Demo qml-user-interaction/ex-key-input/textinputs.qml
User Interaction
Keyboard Input
20/31
Focus Navigation
TextInput { id: name_field ... focus: true KeyNavigation.tab: address_field } TextInput { id: address_field ... KeyNavigation.backtab: name_field }
The name_field item denes KeyNavigation.tab pressing Tab moves focus to the address_field item The address_field item denes KeyNavigation.backtab pressing Shift+Tab moves focus to the name_field item
Demo qml-user-interaction/ex-key-input/tab-navigation.qml
User Interaction
Keyboard Input
21/31
Key Navigation
import QtQuick 1.0 Rectangle { width: 400; height: 200; color: "black" Rectangle { id: left_rect x: 25; y: 25; width: 150; height: 150 color: focus ? "red" : "darkred" KeyNavigation.right: right_rect focus: true } Rectangle { id: right_rect x: 225; y: 25; width: 150; height: 150 color: focus ? "#00ff00" : "green" KeyNavigation.left: left_rect } }
Using cursor keys with non-text items Non-text items can have focus, too
User Interaction
Keyboard Input
22/31
Key Navigation
Rectangle { id: left_rect x: 25; y: 25; width: 150; height: 150 color: focus ? "red" : "darkred" KeyNavigation.right: right_rect focus: true } Rectangle { id: right_rect x: 225; y: 25; width: 150; height: 150 color: focus ? "#00ff00" : "green" KeyNavigation.left: left_rect }
left_rect has the focus initially Dene relationships between left_rect and right_rect using id and KeyNavigation
Demo qml-user-interaction/ex-key-input/key-navigation.qml
User Interaction
Keyboard Input
23/31
Summary
Mouse and cursor input handling: MouseArea receives clicks and other events Use anchors to ll objects and make them clickable Respond to user input:
give the area a name and refer to its properties, or use handlers in the area and change other named items
Key handling: TextInput and TextEdit provide text entry features Set the focus property to start receiving key input Use anchors to make items clickable
lets the user set the focus KeyNavigation denes relationships between items enables focus to be moved using cursor keys, tab and backtab works with non-text-input items
User Interaction Keyboard Input 24/31
User Interaction
Keyboard Input
25/31
User Interaction
Keyboard Input
26/31
User Interaction
Keyboard Input
27/31
User Interaction
Keyboard Input
28/31
User Interaction
Keyboard Input
29/31
Focus Scopes
Focus scopes are used to manage focus for items FocusScope delegates focus to one of its children When the focus scope loses focus remembers which one has the focus When the focus scope gains focus again restores focus to the previously active item
User Interaction
Keyboard Input
30/31
Nokia, Qt and the Nokia and Qt logos are the registered trademarks of Nokia Corporation in Finland and other countries worldwide.
User Interaction
Legal
31/31