54
1 Cross-platform with Qt Ariya Hidayat, Nokia Live Demos

Cross Platform Qt

Embed Size (px)

DESCRIPTION

As a powerful framework, Qt offers tons of modules and classes for building your applications. This talk highlight few practical cross-platform examples of what Qt can do with a fairly few lines of code, ranging from kinetic scrolling, weather service, OpenStreetMap, parallax effect, flight tracking, WYSIWYG HTML editor, and many more. All examples will be accompanied with corresponding live demos.Presentation by Ariya Hidayat held during the Maemo Summit 2009 in Amsterdam

Citation preview

Page 1: Cross Platform Qt

1

Cross-platform with QtAriya Hidayat, Nokia

Live Demos

Page 2: Cross Platform Qt

2

whoami

Open-source Developer(since 2001)

Ph.D. in EE

Page 3: Cross Platform Qt

3

What is Qt?

Best multi-platform application framework!

Page 4: Cross Platform Qt

4

What can Qt do?

Watch the demos!

Page 5: Cross Platform Qt

5

Spread the Love

All (GPL-ed!) examples are available from...

labs.qt.nokia.com

bit.ly/graphicsdojo

Page 6: Cross Platform Qt

6

Example #1

Kinetic Scrolling

Page 7: Cross Platform Qt

7

Flick List

.... lines of Qt/C++ code413

Page 8: Cross Platform Qt

8

Using FlickCharm

QGraphicsView canvas;

FlickCharm charm;charm.activateOn(&canvas);

Page 9: Cross Platform Qt

9

Kinetic Effect State Machine

Steady

Pressed

Manual Scroll

Auto Scroll

Stop

Mouse release

Mouse move

Mouse release

Mouse press

Mousemove

Timer tick

Mouse press

Page 10: Cross Platform Qt

10

Example #2

Weather Info

Page 11: Cross Platform Qt

11

What Should I Wear Today?

.... lines of Qt/C++ code411

Page 12: Cross Platform Qt

12

Simple Animation with Graphics View

item movement &

opacity

Page 13: Cross Platform Qt

13

Using Web Service

http://www.google.com/ig/api?hl=en&weather=oslo

Page 14: Cross Platform Qt

14

Create the Network Request

QUrl url("http://www.google.com/ig/api");url.addEncodedQueryItem("hl", "en");url.addEncodedQueryItem("weather", QUrl::toPercentEncoding(location));

QNetworkAccessManager *manager;manager = new QNetworkAccessManager(this);connect(manager, SIGNAL(finished(QNetworkReply*)), this,SLOT(handleNetworkData(QNetworkReply*)));

manager->get(QNetworkRequest(url));

Page 15: Cross Platform Qt

15

Handle the Network Reply

QUrl url = networkReply->url();data = QString::fromUtf8(networkReply->readAll());networkReply->deleteLater();networkReply->manager()->deleteLater();

Page 16: Cross Platform Qt

16

Parse (or Digest) the XML

QXmlStreamReader xml(data);while (!xml.atEnd()) { xml.readNext(); if (xml.tokenType() == QXmlStreamReader::StartElement) if (xml.name() == "city") city = xml.attributes().value("data").toString()}

Page 17: Cross Platform Qt

17

Scalable Icons with SVG

bitmap vs vector

Page 18: Cross Platform Qt

18

Be Careful with Your SVG

Because one element in a group often

does not make sense!

<g> <ellipse cx="10" cy="10" rx=2" ry="2"/></g>

<ellipse cx="10" cy="10" rx=2" ry="2"/>

Page 19: Cross Platform Qt

19

SVG Loading Time Comparison

Use (free!) tools like:

codedread.com/scour

svgmin.googlecode.com

Page 20: Cross Platform Qt

20

Example #3

Flight Tracking

Page 21: Cross Platform Qt

21

Track Your Flight

.... lines of Qt/C++ code299

Page 22: Cross Platform Qt

22

Web Scraping

• Grab the HTML contents

• Scrap it– remove unnecessary clutters– parse and extract the interesting bits

• Legal aspect– some sites explicitly prohibit the use other than

in a web browser

Page 23: Cross Platform Qt

23

Parsing HTML with XML Reader

• HTML != XML

• Potentially provoke the parser (→ errors)– Solution: “scrub it”, i.e. clean up the raw HTML

// remove all inline frameswhile (true) { i = data.indexOf("<iframe"); if (i < 0) break; data.remove(i, data.indexOf("</iframe>") - i + 8);}

Page 24: Cross Platform Qt

24

Example #4

Magnifying Glass

Page 25: Cross Platform Qt

25

Image Zoom

.... lines of Qt/C++ code197

Page 26: Cross Platform Qt

26

Shadow with Radial Gradient

magnifier

Page 27: Cross Platform Qt

27

Panning with Mouse/Stylus/Finger

void mousePressEvent(QMouseEvent *event) { if (event->buttons() != Qt::LeftButton) return; pressed = true; pressPos = dragPos = event->pos();}

Record the tap position

void mouseMoveEvent(QMouseEvent *event) { if (!event->buttons()) return; QPoint delta = event->pos() - pressPos; pressPos = event->pos(); pan(delta);}

Pan the map based on the movement

Page 28: Cross Platform Qt

28

Example #5

Maps

Page 29: Cross Platform Qt

29

Maps with OpenStreetMap

.... lines of Qt/C++ code450

Page 30: Cross Platform Qt

30

Why OpenStreetMap?

• Free content– Creative Commons Attribution-ShareAlike 2.0

• API does not require the license key• Available in

– rendered images– vector data

More info at www.openstreetmap.org !

Page 31: Cross Platform Qt

31

Getting the Rendered Tile

• Each tile is 256 x 256 pixels

• Zoom level of 0 → the whole world

• Zoom level of 17 → most detailed

QPointF tileForCoordinate(qreal lat, qreal lng, int zoom){ qreal zn = static_cast<qreal>(1 << zoom); qreal tx = (lng + 180.0) / 360.0; qreal ty = (1.0 - log(tan(lat * M_PI / 180.0) + 1.0 / cos(lat * M_PI / 180.0)) / M_PI) / 2.0; return QPointF(tx * zn, ty * zn);}

Page 32: Cross Platform Qt

32

Night Mode

Page 33: Cross Platform Qt

33

Night Mode: Implementation

QPainter p(this);p.setCompositionMode (QPainter::CompositionMode_Difference);p.fillRect(event->rect(), Qt::white);p.end();

Color channel inversion

red = 255 – red

green = 255 – green

blue = 255 - blue

Page 34: Cross Platform Qt

34

Example #6

Parallax Effect

Page 35: Cross Platform Qt

35

Sliding with Parallax

Page 36: Cross Platform Qt

36

Difference in Moving Speed

1 542

3

Page 37: Cross Platform Qt

37

Example #7

WYSIWYG HTML Editor

Page 38: Cross Platform Qt

38

Rich-text Editor with HTML Support

Page 39: Cross Platform Qt

39

Example #8

Instant Messaging

Page 40: Cross Platform Qt

40

Google Chat Client

.... lines of Qt/C++ code140

Page 41: Cross Platform Qt

41

We Cheat (Of Course!)

• Use QWebView from QtWebKit

• Show the “mobile” version

• Add custom login page

Page 42: Cross Platform Qt

42

Example #9

Ray Casting

Page 43: Cross Platform Qt

43

Made Popular by (DOS) Wolfenstein 3-D

.... lines of Qt/C++ code215

Page 44: Cross Platform Qt

44

Memory Access OptimizationMinimize location (in memory) between

two vertically separated pixels

Jump several hundreds bytes

Jump fewbytes only

Page 45: Cross Platform Qt

45

Tight Inner Loop Strategy

while (y1 >= 0 && y2 < bufh && p1 >= 0) { *pixel1 = tex[p1 >> 12]; *pixel2 = tex[p2 >> 12]; p1 -= dy; p2 += dy; --y1; ++y2; pixel1 -= stride; pixel2 += stride;}

Use only simple, 12-bits fixed-point arithmetics

Page 46: Cross Platform Qt

46

Example #10

Special Effects

Page 47: Cross Platform Qt

47

Pixel-based Effect

Page 48: Cross Platform Qt

48

Lighting Example

Page 49: Cross Platform Qt

49

Blur Picker Example

sharp

blurry

Page 50: Cross Platform Qt

50

Fade Message Example

Something will happen

Page 51: Cross Platform Qt

51

Genie Effect

Page 52: Cross Platform Qt

52

Deformation

Page 53: Cross Platform Qt

53

Underwater Effect

Page 54: Cross Platform Qt

54

THANK YOU!

Questions?