1. Qt For Beginners - Part 3
QML and Qt Quick
Eric Magnuson, Qt Consultant
Integrated Computer Solutions
Visit us at http://www.ics.com
Watch the video: http://bit.ly/qt-beginners-qml-quick
Copyright 2016, Integrated Computers Solutions, Inc.
This work may not be reproduced in whole or in part without the express written consent of
Integrated Computer Solutions, Inc.
1
2. β Introduction
β Qt Quick Hello World
β QML Concepts
β Composing User Interfaces
β Nested Elements
β Graphical Types
β Text Items
Agenda
2
4. β Introduction
β Qt Quick Hello World
β QML Concepts
β Composing User Interfaces
β Nested Elements
β Graphical Types
β Text Items
Agenda
4
5. What is QML?
Declarative language for User Interface structure
β Describes the user interface
β What items look like
β How items behave
β UI specified as tree of QML structures with properties
β Elements and identities
β Properties and property binding
5
6. β Introduction
β Qt Quick Hello World
β QML Concepts
β Composing User Interfaces
β Nested Elements
β Graphical Types
β Text Items
Agenda
6
8. β Introduction
β Qt Quick Hello World
β QML Concepts
β Composing User Interfaces
β Nested Elements
β Graphical Types
β Text Items
Agenda
8
9. Properties
Objects are described by properties
β Simple name-value definitions
β width, height, color, ...
β With default values
β Each has a well-defined type
β Separated by semicolons or line breaks
β Used for
β Customizing their appearance
β Changing their behavior
9
10. Types of Properties
β Standard properties can be given values:
Text {
text: "Hello world"
height: 50
}
β Grouped properties keep related properties together
Text {
font.family: "Helvetica"
font.pixelSize: 24
}
β Attached properties are applied to QML structures
TextInput {
text: "Hello world"
KeyNavigation.tab: nextInput
}
β KeyNavigation.tab is not a standard property of TextInput
β Is a standard property that is attached to Items
β Custom properties can be added to any QML type
Rectangle {
property real mass: 100.0
}
10
11. Binding Properties
import QtQuick 2.0
Item {
width: 400; height: 200
Rectangle {
x: 100; y: 50
width: height * 2; height: 100
color: "lightblue"
}
}
β Properties can contain expressions
β See above: width is twice the height
β Not just initial assignments
β Expressions are re-evaluated when needed
11
12. Identifying QML Structures
The id defines an identity of a QML structure
β Lets other QML structures refer to it
β For relative alignment and positioning
β To access or modify an Item's properties
β To re-use common structures (e.g., gradients, images)
β Used to create relationships between structures
β id is not a property
β Not stored in the QObject with other properties
β More like a "label"
β A single Item can have different identities in other
files/scopes.
β parent is a special id referring to the relative
parent structure
12
13. Using Identities
Text {
id: title
x: 50; y: 25
text: "Qt Quick"
font.family: "Helvetica"
font.pixelSize: 50
}
Rectangle {
x: 50; y: 95; height: 5
width: title.width
color: "green"
}
β Text item has the identity, title
β width of Rectangle bound to width of title
β Try using TextInput instead of Text
13
14. Methods
β Most features are accessed via properties
β Methods are also used, to perform actions
β TextInput has a selectAll() method
β Timer has start(), stop() and restart() methods
β Particles has a burst() method
β All methods are public in QML
β Other methods are used to convert values between
types:
β Qt.formatDateTime(datetime, format)
β Qt.md5(data)
β Qt.tint(baseColor, tintColor)
14
15. Property values can have different types:
β Numbers (int and real): 400 and 1.5
β Boolean values: true and false
β Strings: "Hello Qt"
β Constants: AlignLeft
β Lists: [ ... ]
β Lists with one item can be written as just the item itself
β Scripts:
β Included directly in property definitions
β Other types:
β colors, dates, times, rects, points, sizes, 3D vectors, ...
β Usually created using constructors
Basic Types
15
16. β Introduction
β Qt Quick Hello World
β QML Concepts
β Composing User Interfaces
β Nested Elements
β Graphical Types
β Text Items
Agenda
16
17. Why use nested items, anchors and components?
β Concerns separation
β Visual grouping
β Pixel perfect items placing
and layout
β Encapsulation
β Reusability
β Look and feel changes
17
18. β Introduction
β Qt Quick Hello World
β QML Concepts
β Composing User Interfaces
β Nested Elements
β Graphical Types
β Text Items
Agenda
18
22. Images
β Represented by the Image class
β Refer to image files with the source property
β Using absolute URLs or relative to the QML file
import QtQuick 2.0
Rectangle {
width: 400; height: 400
color: "black"
Image {
x: 150; y: 150
source: "../images/rocket.png"
}
}
β width and height are obtained from the image file
β Can be transformed
β Scaled, rotated
β About an axis or central point
22
23. Gradients
Define a gradient using the gradient property:
β As Gradient containing GradientStop values, each with
β A position: a number between 0 (start point) and 1 (end point)
β A color
β The start and end points are on the top and bottom edges of the item
β Gradients override color definitions
import QtQuick 2.0
Rectangle {
width: 400; height: 400
gradient: Gradient {
GradientStop {
position: 0.0; color: "green"
}
GradientStop {
position: 1.0; color: "blue"
}
}
}
β Alternative to gradients: A simple background image.
23
24. β Create border using part of an image:
β Corners (regions 1, 3, 7, 9) are not scaled
β Horizontal borders (2 and 8) are scaled according to horizontalTileMode
β Vertical borders (4 and 6) are scaled according to verticalTileMode
β Middle (5) is scaled according to both modes
β There are 3 different scale modes
β Stretch: scale the image to fit to the available area.
β Repeat: tile the image until there is no more space.
β Round: like Repeat, but scales the images down to ensure that the last
image is not cropped
BorderImage {
source: "content/colors.png"
border { left: 30; top: 30; right: 30; bottom: 30; }
horizontalMode: BorderImage.Stretch
verticalMode: BorderImage.Repeat
...
}
Border Images
24
25. β Introduction
β Qt Quick Hello World
β QML Concepts
β Composing User Interfaces
β Nested Elements
β Graphical Types
β Text Items
Agenda
25
26. Text Items
import QtQuick 2.0
Rectangle {
width: 400; height: 400
color: "lightblue"
Text {
x: 100; y: 100
text: "Qt Quick"
font.family : "Helvetica"
font.pixelSize : 32
}
}
β Width and height determined by the font metrics and text
β Can also use use HTML tags in the text:
"<html><b>Qt Quick</b></html>"
26
27. import QtQuick 2.0
Rectangle {
width: 400; height: 400 color: "lightblue"
TextInput {
x: 50; y: 100; width: 300
text: "Editable text"
font.family : "Helvetica" ; font.pixelSize : 32
}
}
β No decoration (not a QLineEdit widget)
β Gets the focus when clicked
β Need something to click on
β text property changes as the user types
Text Input
27
29. Anchors
β Used to position and align items
β Line up edges or central lines of items
β anchors, an attached property, can specify
β A position in another item: (centerIn, fill)
β An AnchorLine of another item: (left, top)
AnchorLines:
29
30. import QtQuick 2.0
Rectangle {
width: 400; height: 400
color: "lightblue"
id: rectangle1
Text {
text: "Centered text" ; color: "green"
font.family : "Helvetica" ; font.pixelSize : 32
anchors.centerIn : rectangle1
}
}
β anchors.centerIn centers the Text item in
the Rectangle
β Refers to an Item, not an AnchorLine
anchors.centerIn
30
31. import QtQuick 2.0
Rectangle {
// The parent element
width: 400; height: 400
color: "lightblue"
Text {
text: "Centered text" ; color: "green"
font.family : "Helvetica" ; font.pixelSize : 32
anchors.centerIn : parent
}
}
β Each item can refer to its parent Item
β Using the parent ID
β Can refer to ancestors and named children of
ancestors
Anchors and Parent
31
32. Connecting AnchorLines Together
import QtQuick 2.0
Rectangle {
width: 300; height: 100
color: "lightblue"
Text {
y: 34
text: "Right-aligned text" ; color: "green"
font.family : "Helvetica" ; font.pixelSize : 32
anchors.right : parent.right
}
}
β AnchorLines must be parallel
β right to left but not bottom
β AnchorLines of other items are referred to directly:
β Use parent.right, not parent.anchors.right
32
33. Margins
β Used with anchors to add space
β Specify distances
β In pixels
β Between Items connected with anchors
33
35. Hints and Tips β Anchors
β Anchors can only be used with parent and sibling
items
β Anchors work on constraints
β Some items need to have well-defined positions and sizes
β Items without default sizes should be anchored to fixed or well-
defined items
β Anchors creates dependencies on geometries of
other items
β Creates an order in which geometries are calculated
β Avoid creating circular dependencies
β e.g., parent β child β parent
β Margins are only used if the corresponding
anchors are used
β e.g., leftMargin needs left to be defined
35
36. Strategies for Use β Anchors
Identify Items with different roles in the user interface:
β Fixed items
β Make sure these have id defined
β Unless these items can easily be referenced as parent items
β Items that dominate the user interface
β Make sure these have id defined
β Items that react to size changes of the dominant
items
β Give these anchors that refer to the dominant or fixed items
36
38. QML Types
β Item is the base type for Visible QML objects
β Has a position, dimensions
β Usually used to group other visual Items
β Often used as the top-level Item
β Rectangle, Text, TextInput, ...
β Non-visual structures also exist:
β State, Transition, ...
β ListModel, ListElement, Path, ...
β Gradient, Timer, ...
β QQuickItem extends QObject and thus, has properties
β QML Objects can be extended with custom properties from C++
or QML
38
40. Qt Quick Controls
β A set of controls to build entire user interfaces
in Qt Quick
β Originally designed to provide a set of ready to
use components for traditional desktop
applications
β Application Windows, Navigation and Views, Controls,
Menus
β Layouts
β System and Theme integration
β Idea and design extensible to touchscreen
applications
40
41. Qt Quick Controls
β Platforms supported include Windows, OS X,
Android, Linux and iOS
β Qt Quick Controls 1 are designed for desktop
platforms and look like native widgets
β Qt Quick Controls 2 (aka Qt Labs Controls) are
designed for embedded platforms and are
lighter weight
β Controls have similar APIs
41
43. Mouse Areas
Mouse areas define 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
43
46. Mouse Area Hints and Tips
β A mouse area only responds to its
acceptedButtons
β The handlers are not called for other buttons, but
β Any click involving an allowed button is reported
β The pressedButtons property contains all buttons
β Even non-allowed buttons, if an allowed button is also
pressed
β With hoverEnabled set to false
β containsMouse can be true if the mouse area is clicked
46
48. Touch Events
QML types that already recognize Touch events:
β Single-touch (MouseArea)
β Multi-touch (MultiPointTouchArea)
β Gestures:
β Swipe (Flickable, ListView)
β Pinch (PinchArea)
β Tap and Hold (MouseArea onPressAndHold)
48
49. Multi-Touch Events
MultiPointTouchArea {
anchors.fill : parent
touchPoints : [
TouchPoint { id: point1 }, TouchPoint { id: point2 }, TouchPoint { id: point3 }
]
}
TouchPoint properties:
β int x
β int y
β bool pressed
β int pointId
49
50. MultiPointTouchArea Signals
β onPressed(list<TouchPoint> touchPoints) onReleased(β¦)
touchPoints is list of changed points.
β onUpdated(β¦)
Called when points is updated (moved) touchPoints is list of changed
points.
β onTouchUpdated(β¦)
Called on any change touchPoints is list of all points.
50
51. MultiPointTouchArea Signals
β onGestureStarted(GestureEvent gesture)
Cancel the gesture using gesture.cancel()
β onCanceled(list<TouchPoint> touchPoints)
Called when another Item takes over touch handling.
Useful for undoing what was done on onPressed.
51
52. Swipe Gestures
β Built into Flickable, ListView
β snapMode: ListView.SnapOneItem
The view settles no more than one item away from the first visible item at
the time the mouse button is released.
β orientation: ListView.Horizontal
52
54. Pinch Gestures
β Signals for manual pinch handling
β onPinchStarted(PinchEvent pinch)
β onPinchUpdated(PinchEvent pinch)
β onPinchFinished()
β PinchEvent properties:
β point1, point2, center
β rotation
β scale
β accepted
Set to false in the onPinchStarted handler if the gesture should
not be handled
54
56. Keyboard Input
Basic keyboard input is handled in two different
use cases:
β Accepting text input
β TextInput and TextEdit
β Navigation between Items
β Changing the focused Item
β Directional (arrow keys), tab and backtab
56
57. Raw Keyboard Input
β Raw key input can be handled by items
β With predefined handlers for commonly used keys
β Full key event information is also available
β The same focus mechanism is used as for
ordinary text input
β Enabled by setting the focus property
β Key handling is not an inherited property of items
β Enabled using the Keys attached property
β Key events can be forwarded to other objects
β Enabled using the Keys.forwardTo attached property
β Accepts a list of objects
57