Tampere MeeGo Meetup - Qt Quick

38
Qt Quick Basic introduction Juha Ristolainen / Futurice Oy @Riussi on Twitter Tampere MeeGo Meetup on 23rd November 2010

description

Short introduction to Qt Quick at Tampere MeeGo Meetup meeting on 23rd November 2010.

Transcript of Tampere MeeGo Meetup - Qt Quick

Page 1: Tampere MeeGo Meetup - Qt Quick

Qt Quick

Basic introduction Juha Ristolainen / Futurice Oy

@Riussi on Twitter

Tampere MeeGo Meetup on 23rd November 2010

Page 2: Tampere MeeGo Meetup - Qt Quick

What?

• Qt Quick is an umbrella term: – Qt Metaobject Language (QML)

– Language runtime integrated with Qt

– QtDeclarative C++ module in Qt

– Qt Creator IDE-support and graphical designer (2.1)

• Qt UI Creation Kit

• Complete toolkit for creating dynamic custom UIs

Page 3: Tampere MeeGo Meetup - Qt Quick

Why?

• Rapid UI prototyping – Creating and changing UIs has been difficult in the

past – No compilation step needed, easy and fast to modify

and test

• Design oriented – Accessible tools for designers, easy to use – ”Blank canvas”

• Qt Quick Components will provide basic components

– Lightweight and customizable components

• Intuitive and great looking user interfaces

Page 4: Tampere MeeGo Meetup - Qt Quick

QML

• QML is a new declarative language

• QML describes the user interface

– Described as a tree of elements with properties

• Compose UIs from elements

Text Image

Rectangle

Page 5: Tampere MeeGo Meetup - Qt Quick

Elements

• Elements are the basic structures in QML

• QML provides only a set of basic elements

– Rectangle, Text, Image, ...

– Also non-visible elements

• Item, States, transitions, models, paths, timers, ...

• Elements contain properties

– Can be extended with custom properties

Page 6: Tampere MeeGo Meetup - Qt Quick

Properties

• Elements are described by properties

• Simple name-value definitions with default values

• Used for:

– Customizing appearance

– Changing behaviour

• Bindings

– Identifying elements

Page 7: Tampere MeeGo Meetup - Qt Quick

Properties, pt 2

• Id-property – Used for referencing elements

• Standard properties – Width, height, color, ...

• Grouped properties – Font.family, font.pixelSize

• Attached properties – Properties that elements don’t have by default but can be

attached – KeyNavigation.tab: anotherElement

• Custom properties – Property string username: ””

Page 8: Tampere MeeGo Meetup - Qt Quick

QML: Basic types

• Property values can have different types: – Numbers

– Boolean

– Strings

– Constants

– Lists

– Scripts

– Other basic type: colors, dates, times, rects, points, sizes

Page 9: Tampere MeeGo Meetup - Qt Quick

Property binding

• Property values can contain expressions

• Expressions can be blocks of Javascript

• Expressions are evaluated when needed

– Uses Qt signals/slots

Page 10: Tampere MeeGo Meetup - Qt Quick

Images

• Image

– Source-property is network transparent

– Can be easily transformed

• BorderImage

– Define stretchable areas on image for scalable assets

– Similar to Android’s 9-patch

Page 11: Tampere MeeGo Meetup - Qt Quick

Gradients

• Gradient-property

– Add two or more GradientStops

• Position between 0.0 and 1.0

• Color

– Using gradients is CPU-intensive

• Preferable to use images

Page 12: Tampere MeeGo Meetup - Qt Quick

Layouts

• Used to position and align elements

– Line up edges or central lines of elements

• Dynamic layouts

– Avoid hardcoded values, use anchors and bindings. Remember when using the graphical editor

• Anchor-based

Page 13: Tampere MeeGo Meetup - Qt Quick

Anchors

• Anchors – Can refer to other elements

• centerIn, fill, ...

– Can refer to anchors of other elements • Left, right, top, bottom, verticalCenterm ...

• Margins – Specify margins between elements connected with

anchors

• Constraints – Some items need to be well-defined – Avoid circular dependencies

Page 14: Tampere MeeGo Meetup - Qt Quick

Hello World import Qt 4.7

Rectangle {

id: mainRect

width: 500; height: 500

color: "lightblue" // color takes SVG colornames as well

// Add an image in the middle of the rectangle

Image {

id: faceImage

source: "face.png"

smooth: true

height: parent.height / 2; width: parent.width / 2

anchors.centerIn: parent // refer to parent or use mainRect

}

// Put a text field underneath it

Text {

anchors.top: faceImage.bottom; anchors.topMargin: 20

anchors.horizontalCenter: faceImage.horizontalCenter

text: qsTr("Hello, World")

font.family: "Nokia Sans”; font.pixelSize: 32

}

}

Page 15: Tampere MeeGo Meetup - Qt Quick

Demo

• 01-basics

Page 16: Tampere MeeGo Meetup - Qt Quick

User input

• Mouse / touch

• Keyboard

Page 17: Tampere MeeGo Meetup - Qt Quick

Mouse/touch input

• MouseArea

– Defines areas on the screen where touch input occurs

– Layout like any other item

• Ways to handle input

– Signals:

• onClicked, onEntered, ...

– Property bindings:

• mouseArea.containsMouse

• GestureArea in Labs

Page 18: Tampere MeeGo Meetup - Qt Quick

Keyboard input

• Two elements: – TextInput

– TextEdit

• No decorations. Need to create using other elements

• Navigation between elements – Changing focus:

• Clicking

• Attached properties Keys and KeyNavigation

• Raw keyboard input – Predefined handlers for common keys: e.g. onLeftPressed

Page 19: Tampere MeeGo Meetup - Qt Quick

Demo

• 02-input

Page 20: Tampere MeeGo Meetup - Qt Quick

States

• Items can define states

• State is a set of property values for an item

– States-property: list of item’s named states

– State-property: current state

• Property values are set when entering a state

Page 21: Tampere MeeGo Meetup - Qt Quick

State changes

• Explicitly on user interaction

– MouseArea events, TextInput events

• Let the state decide

– When-property

– Make sure when is true only for one state at a time

Page 22: Tampere MeeGo Meetup - Qt Quick

Transitions

• Transitions define how items change when switching states

• Usually for animating state changes

• From and to-properties

– Wildcard * allowed

• Reversible transitions

– Used with when

Page 23: Tampere MeeGo Meetup - Qt Quick

Animations

• Can be applied to any visible elements • Animations update element properties to cause a

visual change • Specialized types

– NumberAnimation – ColorAnimation – RotationAnimation – Vector3dAnimation – ...

• Easing curves for variable speed animations

Page 24: Tampere MeeGo Meetup - Qt Quick

Multiple animations

• SequentialAnimation

– Run animations in sequence

• ParallelAnimation

– Run animations in parallel

• PauseAnimation

– Insert pauses into animation groups

Page 25: Tampere MeeGo Meetup - Qt Quick

Demo

• 03-states

Page 26: Tampere MeeGo Meetup - Qt Quick

Data models

• Pure models

– ListModel

– XmlListModel

• Visual models

– Combine information on how to show data with data

– VisualItemModel

– VisualDataModel

Page 27: Tampere MeeGo Meetup - Qt Quick

ListModel

• Contains simple sequences of elements – No information on how to display data. Data view

items need to define a delegate for showing data

• ListElement – No pre-defined properties, define all properties

yourself

• Can be used with ListViews, Repeaters, etc.

• Dynamic list of items – Can be operated on with methods like append,

remove, move

Page 28: Tampere MeeGo Meetup - Qt Quick

Data views

• ListView

• GridView

• PathView

– Define a path where to show data from the model

Page 29: Tampere MeeGo Meetup - Qt Quick

ListView

• Model for data, Delegate for drawing (no default delegate

• Flickable surface with no decorations

• Header, footer, highlight

• Current item

• Sections

– Categorize and sort on some property

Page 30: Tampere MeeGo Meetup - Qt Quick

XmlListModel

• Maps XML data to model properties using Xpath-queries

• Define a source for XML data

– local or network

• Define a query for single item of data in XML

• XmlRole

– Query different pieces of data from single item

Page 31: Tampere MeeGo Meetup - Qt Quick

// XmlListModel getting data from a public RSS-feed

XmlListModel {

id: listModel

source: "http://labs.qt.nokia.com/feed/"

query: "/rss/channel/item"

XmlRole {

name: "title"; query: "title/string()"

}

XmlRole {

name: "pubDate"; query: "pubDate/string()"

}

XmlRole {

name: "link"; query: "link/string()”

}

XmlRole {

name: "description"; query: "description/string()”

}

}

Page 32: Tampere MeeGo Meetup - Qt Quick

Demo

• 04-listview

Page 33: Tampere MeeGo Meetup - Qt Quick

Demo

• 05-xmllistmodel

Page 34: Tampere MeeGo Meetup - Qt Quick

Not covered

• Defining custom components – In pure QML or C++

• QML C++ integration – Creating QML-plugins with C++ – Expose QObjects to QML, use signals/slots

• Embed QML inside C++ application • For bigger projects think about using a C++

”engine” and QML for UI • Qt Mobility QML bindings (Maps, Location,

Gallery, etc)

Page 35: Tampere MeeGo Meetup - Qt Quick

What are you waiting for?

• Get started today

• All you need:

– Qt 4.7

– Qt Creator 2.1 (beta currently available)

• For mobile development Nokia Qt SDK

Page 36: Tampere MeeGo Meetup - Qt Quick

More information

• Qt 4.7 documentation: http://doc.qt.nokia.com/4.7/index.html

• Qt Developer Network: http://developer.qt.nokia.com/

Page 38: Tampere MeeGo Meetup - Qt Quick

Thank you

• Juha Ristolainen

– Senior Consultant at Futurice Oy

– http://www.futurice.com/

– @Riussi on Twitter