summaryrefslogtreecommitdiff
path: root/doc/src/examples/weatherinfo.qdoc
blob: b517785b1e087db0d5f6cc85bee13f404ddea963 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** GNU Free Documentation License
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms
** and conditions contained in a signed written agreement between you
** and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \example weatherinfo
    \title Weather Info (C++/QML)

    \brief The Weather Info example shows how to use the user's current position
           to retrieve local content from a web service in a C++ plugin for QML.

    \ingroup qtlocation-examples

    Key QtLocation classes used in this example:

    \list
    \o \l{QGeoPositionInfo}
    \o \l{QGeoPositionInfoSource}
    \endlist

    \image ../images/example-weatherinfo.png

    The key part of this example is the application's data model, contained
    in the WeatherData and AppModel classes. WeatherData represents the weather
    information taken from the HTTP service. It is a simple data class, but we
    give it Q_PROPERTies to expose it nicely to QML, later.

    \snippet examples/weatherinfo/appmodel.h 0
    \snippet examples/weatherinfo/appmodel.h 1

    AppModel models the state of the entire application. At startup, the
    application first begins by waiting for network connectivity. We do
    this using the QNetworkConfigurationManager and QNetworkSession family
    of C++ APIs.

    \snippet examples/weatherinfo/appmodel.cpp 0
    \snippet examples/weatherinfo/appmodel.cpp 1

    Once the network session is open, we proceed to get the platform's
    default position source using QGeoPositionInfo::createDefaultSource()

    \snippet examples/weatherinfo/appmodel.cpp 2

    If no default source is available, we take a static location and fetch
    weather for that. If, however, we do have a position source, we connect
    its positionUpdated() signal to a slot on the AppModel and call
    startUpdates(), which begins regular updates of device position.

    When a position update is received, we use the longitude and latitude
    of the returned coordinate to retrieve the current "city" name for use
    in the weather lookup.

    \snippet examples/weatherinfo/appmodel.cpp 3

    To inform the UI about this process, the cityChanged() signal is emitted
    when a new city is used, and the weatherChanged() signal whenever a
    weather update occurs.

    \snippet examples/weatherinfo/appmodel.h 2
    \snippet examples/weatherinfo/appmodel.h 3
    \snippet examples/weatherinfo/appmodel.h 4

    We use a QDeclarativeListProperty for the weather forecast information,
    which contains the next 4 days of forecast weather. This makes it
    easy to access from QML.

    To expose these to the QML UI layer, we use the qmlRegisterType()
    function. We call this once for each type we wish to register, before
    loading the actual QML file.

    \snippet examples/weatherinfo/main.cpp 0
    \snippet examples/weatherinfo/main.cpp 1

    Finally, in the actual QML, we instantiate the AppModel.

    \snippet examples/weatherinfo/weatherinfo.qml 0
    \snippet examples/weatherinfo/weatherinfo.qml 1
    \snippet examples/weatherinfo/weatherinfo.qml 2

    Once instantiated like this, we can use its properties elsewhere in the
    QML document:

    \snippet examples/weatherinfo/weatherinfo.qml 3
    \snippet examples/weatherinfo/weatherinfo.qml 4

*/