7 QML Presenting Data
7 QML Presenting Data
Presenting Data
Contents
Arranging Items
Data Models
Using Views
XML Models
Views Revisited
2015
Objectives
Can manipulate and present data:
Familiarity with positioners and repeaters
Pure models
Visual models
XML models
2015
2015
Arranging Items
Arranging Items
Positioners and repeaters make it easier to work with many items
Positioners arrange items in standard layouts
In a column: Column
In a row: Row
In a grid: Grid
Like words on a page: Flow
For use with positioners
Using data from a model
2015
Positioning Items
Grid {
x: 15; y: 15; width: 300; height: 300
columns: 2; rows: 2; spacing: 20
Rectangle { width: 125; height: 125; color:
Rectangle { width: 125; height: 125; color:
Rectangle { width: 125; height: 125; color:
Rectangle { width: 125; height: 125; color:
}
"red" }
"green" }
"silver" }
"blue" }
Ina 2 by 2 Grid
With horizontal/vertical spacing of 20 pixels
2015
Repeating Items
Rectangle { width: 400; height: 400; color: "black"
Grid { x: 5; y: 5 rows: 5; columns: 5; spacing: 10
Repeater {
model: 24
Rectangle { width: 70; height: 70 color: "lightgreen" }
}
}
}
2015
Repeating Items
Rectangle { width: 400; height: 400; color: "black"
Grid { x: 5; y: 5 rows: 5; columns: 5; spacing: 10
Repeater {
model: 24
Rectangle {
width: 70; height: 70 color: "lightgreen" }
}
}
}
Demo: qml-presenting-data/ex-arranging-items/repeater-gird.qml
9
2015
Indexing Items
Rectangle { width: 400; height: 400; color: "black"
Grid { x: 5; y: 5 rows: 5; columns: 5; spacing: 10
Repeater {
model: 24
Rectangle {
width: 70; height: 70 color: "lightgreen"
Text {
text: index
font.pointSize: 30
anchors.centerIn: parent }
}
}
}
}
10
2015
11
2015
12
2015
Lab Calendar
13
2015
Data Models
15
Using delegates
2015
Models
Pure models provide access to data:
ListModel
XmlListModel
Visual models provide information about how to display data:
Visual item model: ObjectModel (replaces VisualItemModel)
2015
List Models
ListModel {
id: nameModel
ListElement { }
ListElement { }
ListElement { }
}
17
2015
"Alice" }
"Bob" }
"Jane" }
"Victor" }
"Wendy" }
Dene a ListModel
name:
name:
name:
name:
name:
Demo: qml-presenting-data/ex-models-views/list-model-list-view.qml
18
2015
Dening a Delegate
Component {
id: nameDelegate
Text {
text: name;
font.pixelSize: 32
}
}
19
20
2015
21
2015
22
2015
23
24
2015
25
2015
Presenting Data
Views
27
2015
List Views
Take the model and delegate from before:
ListModel {
id: nameModel
ListElement {
ListElement {
ListElement {
ListElement {
ListElement {
}
name:
name:
name:
name:
name:
"Alice" }
"Bob" }
"Jane" }
"Victor" }
"Wendy" }
Component {
id: nameDelegate
Text {
text: name;
font.pixelSize: 32
}
}
28
2015
List Views
ListView {
anchors.fill: parent
model: nameModel
delegate: nameDelegate
clip: true
}
No default delegate
Unclipped views paint outside their areas
Demo: qml-presenting-data/ex-models-views/list-model-list-view.qml
29
2015
By default, ListView is
To add decoration:
Undecorated
A ickable surface (can be dragged and icked)
With a header and footer
With a highlight item to show the current item
Demo: qml-presenting-data/ex-models-views/list-view-decoration.qml
30
2015
31
2015
Demo: qml-presenting-data/ex-models-views/list-view-current.item.qml
32
2015
Adding Sections
ListModel {
id: nameModel
ListElement {
ListElement {
ListElement {
ListElement {
ListElement {
}
33
name:
name:
name:
name:
name:
2015
Displaying Sections
Using the ListView
Set section.property
Set section.delegate
34
2015
Displaying Sections
ListView {
model: nameModel
section.property: "team
section.criteria: ViewSection.FullString
section.delegate: Rectangle {
color: "#b0dfb0"
width: parent.width
height: childrenRect.height + 4
Text { anchors.horizontalCenter: parent.horizontalCenter
font.pixelSize: 16
font.bold: true
text: section }
}
}
35
2015
Grid Views
file:
file:
file:
file:
Demo: qml-presenting-data/ex-models-views/list-model-grid-view.qml
36
2015
Grid Views
Set up a delegate:
Component {
id: nameDelegate
Column {
Image {
id: delegateImage
anchors.horizontalCenter: delegateText.horizontalCenter
source: file; width: 64; height: 64; smooth: true
fillMode: Image.PreserveAspectFit
}
Text {
id: delegateText
text: name; font.pixelSize: 24
}
}
}
37
2015
Grid Views
GridView {
anchors.fill: parent
model: nameModel
delegate: nameDelegate
clip: true
}
38
2015
To add decoration:
Demo: qml-presenting-data/ex-models-views/grid-view-decoration.qml
39
2015
header: Rectangle {
width: parent.width; height: 10
color: "pink"
}
footer: Rectangle {
width: parent.width; height: 10
color: "lightblue"
}
highlight: Rectangle {
width: parent.width
color: "lightgray"
}
focus: true clip: true
}
40
2015
Lab Contacts
41
A name property
A file property referring to an image
2015
XML Models
43
2015
44
2015
XML Roles
45
2015
Demo: qml-presenting-data/ex-models-views/TitleDelegate.qml
46
2015
Dening a Delegate
Component {
Item {
width: parent.width; height: 64
Rectangle {
width: Math.max(childrenRect.width + 16, parent.width)
height: 60; clip: true
color: "#505060"; border.color: "#8080b0"; radius: 8
Column {
Text { x: 6; color: "white"
font.pixelSize: 32; text: title }
Text { x: 6; color: "white"
font.pixelSize: 16; text: link }
}
}
}
}
47
Views Revisited
Customizing Views
49
2015
Customizing Views
ListView {
preferredHighlightBegin: 42
preferredHighlightEnd: 150
highlightRangeMode: ListView.ApplyRange
Demo: qml-presenting-data/ex-models-views/list-view-highlight-range-apply.qml
50
2015
Customizing Views
ListView {
preferredHighlightBegin: 42
preferredHighlightEnd: 150
highlightRangeMode:
ListView.StrictlyEnforceRange
Demo: qml-presenting-data/ex-models-views/list-view-highlight-range-strict.qml
51
2015
Optimizing Views
52
2015