summaryrefslogtreecommitdiff
path: root/examples/positioning/weatherinfo/doc/src/weatherinfo.qdoc
blob: 7d9cab6feba2ee2179867a40dc286572a4bd5bc0 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Free Documentation License Usage
** 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. Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: https://www.gnu.org/licenses/fdl-1.3.html.
** $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 qtpositioning-examples

    Key \l{Qt Positioning} classes used in this example:

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

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

    \include examples-run.qdocinc

    \section1 Weather Data Providers

    The example uses two unrelated weather data providers:

    \list
    \li \l {http://www.openweathermap.org}{OpenWeather}
    \li \l {https://www.weatherapi.com/}{WeatherAPI.com}
    \endlist

    The provider to be used is selected automatically at runtime and can be
    changed if the selected provider is not available. However, it can't be
    specified manually.

    \note Free plans are used for both providers, which implies certain
    limitations on the amount of weather requests. If the limits are exceeded,
    the providers become temporary unavailable. When both providers are
    unavailable, the application would not be able to show any weather
    information. In this case it is required to wait until at least one of the
    providers becomes available again.

    \section1 Application Data Models

    The key part of this example is the application's data model, contained
    in the \c WeatherData and \c AppModel classes. \c WeatherData represents
    the weather information taken from the HTTP service. It is a simple data
    class, but we use \l Q_PROPERTY to expose it nicely to QML later.
    It also uses \l QML_ANONYMOUS macro, which makes it recognized in QML.

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

    \c AppModel models the state of the entire application. At startup, we
    get the platform's default position source using
    \l QGeoPositionInfoSource::createDefaultSource().

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

    If no default source is available, we take a static position and fetch
    weather for that. If, however, we do have a position source, we connect
    its \l {QGeoPositionInfoSource::}{positionUpdated()} signal to a slot on the
    \c AppModel and call \l {QGeoPositionInfoSource::}{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 weather data for the specified
    location.

    \snippet weatherinfo/appmodel.cpp 2

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

    The model also uses \l QML_ELEMENT macro, which makes it available in QML.

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

    We use a \l QQmlListProperty for the weather forecast information,
    which contains the weather forecast for the next days (the number of days
    is provider-specific). This makes it easy to access the forecast from QML.

    \section1 Expose Custom Models to QML

    To expose the models to the QML UI layer, we use the \l QML_ELEMENT and
    \l QML_ANONYMOUS macros. See the \l QQmlEngine class description for more
    details on these macros.

    To make the types available in QML, we need to update our build accordingly.

    \section2 CMake Build

    For a CMake-based build, we need to add the following to the
    \c {CMakeLists.txt}:

    \quotefromfile weatherinfo/CMakeLists.txt
    \skipto qt_add_qml_module(weatherinfo
    \printuntil )

    \section2 qmake Build

    For a qmake build, we need to modify the \c {weatherinfo.pro} file in the
    following way:

    \quotefromfile weatherinfo/weatherinfo.pro
    \skipto CONFIG
    \printuntil QML_IMPORT_MAJOR_VERSION

    \section1 Instantiate the Models in QML

    Finally, in the actual QML, we instantiate the \c {AppModel}:

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

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

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

    \section1 Files and Attributions

    The example bundles the following images from Third-Party sources:

    \table
    \row
        \li \l{weatherinfo-tango-icons}{Tango Icons}
        \li Public Domain
    \row
        \li \l{weatherinfo-tango-weather-pack}{Tango Weather Icon Pack by Darkobra}
        \li Public Domain
    \endtable
*/