/usr/share/qt5/doc/qtpositioning/qtpositioning-weatherinfo-example.html is in qtpositioning5-doc-html 5.3.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- weatherinfo.qdoc -->
<title>Weather Info (C++/QML) | QtPositioning 5.3</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<ul>
<li>Qt 5.3</li>
<li><a href="qtpositioning-index.html">Qt Positioning</a></li>
<li>Weather Info (C++/QML)</li>
<li id="buildversion">
Qt 5.3.2 Reference Documentation</li>
</ul>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#running-the-example">Running the Example</a></li>
</ul>
</div>
<h1 class="title">Weather Info (C++/QML)</h1>
<span class="subtitle"></span>
<!-- $$$weatherinfo-description -->
<div class="descr"> <a name="details"></a>
<p>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.<p>Key <a href="qtpositioning-index.html">Qt Positioning</a> classes used in this example:</p>
<ul>
<li><a href="qgeopositioninfo.html">QGeoPositionInfo</a></li>
<li><a href="qgeopositioninfosource.html">QGeoPositionInfoSource</a></li>
</ul>
<p class="centerAlign"><img src="images/example-weatherinfo.png" alt="" /></p><a name="running-the-example"></a>
<h2>Running the Example</h2>
<p>To run the example from <a href="http://qt-project.org/doc/qtcreator/index.html">Qt Creator</a>, open the <b>Welcome</b> mode and select the example from <b>Examples</b>. For more information, visit <a href="http://qt-project.org/doc/qtcreator/creator-build-example-application.html">Building and Running an Example</a>.</p>
<p>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.</p>
<pre class="cpp"><span class="keyword">class</span> WeatherData : <span class="keyword">public</span> <span class="type">QObject</span> {
Q_OBJECT
Q_PROPERTY(<span class="type">QString</span> dayOfWeek
READ dayOfWeek WRITE setDayOfWeek
NOTIFY dataChanged)
Q_PROPERTY(<span class="type">QString</span> weatherIcon
READ weatherIcon WRITE setWeatherIcon
NOTIFY dataChanged)
Q_PROPERTY(<span class="type">QString</span> weatherDescription
READ weatherDescription WRITE setWeatherDescription
NOTIFY dataChanged)
Q_PROPERTY(<span class="type">QString</span> temperature
READ temperature WRITE setTemperature
NOTIFY dataChanged)
<span class="keyword">public</span>:
<span class="keyword">explicit</span> WeatherData(<span class="type">QObject</span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
WeatherData(<span class="keyword">const</span> WeatherData <span class="operator">&</span>other);
<span class="type">QString</span> dayOfWeek() <span class="keyword">const</span>;
<span class="type">QString</span> weatherIcon() <span class="keyword">const</span>;
<span class="type">QString</span> weatherDescription() <span class="keyword">const</span>;
<span class="type">QString</span> temperature() <span class="keyword">const</span>;
<span class="type">void</span> setDayOfWeek(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>value);
<span class="type">void</span> setWeatherIcon(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>value);
<span class="type">void</span> setWeatherDescription(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>value);
<span class="type">void</span> setTemperature(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>value);
<span class="keyword">signals</span>:
<span class="type">void</span> dataChanged();
};</pre>
<p>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.</p>
<pre class="cpp">AppModel<span class="operator">::</span>AppModel(<span class="type">QObject</span> <span class="operator">*</span>parent) :
<span class="type">QObject</span>(parent)<span class="operator">,</span>
d(<span class="keyword">new</span> AppModelPrivate)
{
<span class="comment">// make sure we have an active network session</span>
d<span class="operator">-</span><span class="operator">></span>nam <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QNetworkAccessManager</span>(<span class="keyword">this</span>);
<span class="type">QNetworkConfigurationManager</span> ncm;
d<span class="operator">-</span><span class="operator">></span>ns <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QNetworkSession</span>(ncm<span class="operator">.</span>defaultConfiguration()<span class="operator">,</span> <span class="keyword">this</span>);
connect(d<span class="operator">-</span><span class="operator">></span>ns<span class="operator">,</span> SIGNAL(opened())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(networkSessionOpened()));
<span class="comment">// the session may be already open. if it is, run the slot directly</span>
<span class="keyword">if</span> (d<span class="operator">-</span><span class="operator">></span>ns<span class="operator">-</span><span class="operator">></span>isOpen())
<span class="keyword">this</span><span class="operator">-</span><span class="operator">></span>networkSessionOpened();
<span class="comment">// tell the system we want network</span>
d<span class="operator">-</span><span class="operator">></span>ns<span class="operator">-</span><span class="operator">></span>open();
}</pre>
<p>Once the network session is open, we proceed to get the platform's default position source using QGeoPositionInfo::createDefaultSource()</p>
<pre class="cpp"><span class="type">void</span> AppModel<span class="operator">::</span>networkSessionOpened()
{
d<span class="operator">-</span><span class="operator">></span>src <span class="operator">=</span> <span class="type"><a href="qgeopositioninfosource.html">QGeoPositionInfoSource</a></span><span class="operator">::</span>createDefaultSource(<span class="keyword">this</span>);
<span class="keyword">if</span> (d<span class="operator">-</span><span class="operator">></span>src) {
d<span class="operator">-</span><span class="operator">></span>useGps <span class="operator">=</span> <span class="keyword">true</span>;
connect(d<span class="operator">-</span><span class="operator">></span>src<span class="operator">,</span> SIGNAL(positionUpdated(<span class="type"><a href="qgeopositioninfo.html">QGeoPositionInfo</a></span>))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(positionUpdated(<span class="type"><a href="qgeopositioninfo.html">QGeoPositionInfo</a></span>)));
connect(d<span class="operator">-</span><span class="operator">></span>src<span class="operator">,</span> SIGNAL(error(<span class="type"><a href="qgeopositioninfosource.html">QGeoPositionInfoSource</a></span><span class="operator">::</span>Error))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(positionError(<span class="type"><a href="qgeopositioninfosource.html">QGeoPositionInfoSource</a></span><span class="operator">::</span>Error)));
d<span class="operator">-</span><span class="operator">></span>src<span class="operator">-</span><span class="operator">></span>startUpdates();
} <span class="keyword">else</span> {
d<span class="operator">-</span><span class="operator">></span>useGps <span class="operator">=</span> <span class="keyword">false</span>;
d<span class="operator">-</span><span class="operator">></span>city <span class="operator">=</span> <span class="string">"Brisbane"</span>;
<span class="keyword">emit</span> cityChanged();
<span class="keyword">this</span><span class="operator">-</span><span class="operator">></span>refreshWeather();
}
}</pre>
<p>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 positionUpdated() signal to a slot on the AppModel and call startUpdates(), which begins regular updates of device position.</p>
<p>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.</p>
<pre class="cpp"><span class="type">void</span> AppModel<span class="operator">::</span>positionUpdated(<span class="type"><a href="qgeopositioninfo.html">QGeoPositionInfo</a></span> gpsPos)
{
d<span class="operator">-</span><span class="operator">></span>coord <span class="operator">=</span> gpsPos<span class="operator">.</span>coordinate();
<span class="keyword">if</span> (<span class="operator">!</span>(d<span class="operator">-</span><span class="operator">></span>useGps))
<span class="keyword">return</span>;
<span class="type">QString</span> latitude<span class="operator">,</span> longitude;
longitude<span class="operator">.</span>setNum(d<span class="operator">-</span><span class="operator">></span>coord<span class="operator">.</span>longitude());
latitude<span class="operator">.</span>setNum(d<span class="operator">-</span><span class="operator">></span>coord<span class="operator">.</span>latitude());</pre>
<p>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.</p>
<pre class="cpp"><span class="keyword">class</span> AppModel : <span class="keyword">public</span> <span class="type">QObject</span>
{
Q_OBJECT
Q_PROPERTY(bool ready
READ ready
NOTIFY readyChanged)
Q_PROPERTY(bool hasSource
READ hasSource
NOTIFY readyChanged)
Q_PROPERTY(bool hasValidCity
READ hasValidCity
NOTIFY cityChanged)
Q_PROPERTY(bool hasValidWeather
READ hasValidWeather
NOTIFY weatherChanged)
Q_PROPERTY(bool useGps
READ useGps WRITE setUseGps
NOTIFY useGpsChanged)
Q_PROPERTY(<span class="type">QString</span> city
READ city WRITE setCity
NOTIFY cityChanged)
Q_PROPERTY(WeatherData <span class="operator">*</span>weather
READ weather
NOTIFY weatherChanged)
Q_PROPERTY(<span class="type">QQmlListProperty</span><span class="operator"><</span>WeatherData<span class="operator">></span> forecast
READ forecast
NOTIFY weatherChanged)
<span class="keyword">public</span>:
<span class="keyword">explicit</span> AppModel(<span class="type">QObject</span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
<span class="operator">~</span>AppModel();
bool ready() <span class="keyword">const</span>;
bool hasSource() <span class="keyword">const</span>;
bool useGps() <span class="keyword">const</span>;
bool hasValidCity() <span class="keyword">const</span>;
bool hasValidWeather() <span class="keyword">const</span>;
<span class="type">void</span> setUseGps(bool value);
<span class="type">QString</span> city() <span class="keyword">const</span>;
<span class="type">void</span> setCity(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>value);
WeatherData <span class="operator">*</span>weather() <span class="keyword">const</span>;
<span class="type">QQmlListProperty</span><span class="operator"><</span>WeatherData<span class="operator">></span> forecast() <span class="keyword">const</span>;
<span class="keyword">public</span> <span class="keyword">slots</span>:
Q_INVOKABLE <span class="type">void</span> refreshWeather();
<span class="keyword">signals</span>:
<span class="type">void</span> readyChanged();
<span class="type">void</span> useGpsChanged();
<span class="type">void</span> cityChanged();
<span class="type">void</span> weatherChanged();
};</pre>
<p>We use a QQmlListProperty for the weather forecast information, which contains the next 4 days of forecast weather. This makes it easy to access from QML.</p>
<p>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.</p>
<pre class="cpp"><span class="preprocessor">#include "appmodel.h"</span>
<span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>)
{
<span class="type">QGuiApplication</span> application(argc<span class="operator">,</span> argv);
qmlRegisterType<span class="operator"><</span>WeatherData<span class="operator">></span>(<span class="string">"WeatherInfo"</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="string">"WeatherData"</span>);
qmlRegisterType<span class="operator"><</span>AppModel<span class="operator">></span>(<span class="string">"WeatherInfo"</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="string">"AppModel"</span>);
<span class="keyword">const</span> <span class="type">QString</span> mainQmlApp <span class="operator">=</span> QLatin1String(<span class="string">"qrc:///weatherinfo.qml"</span>);
<span class="type">QQuickView</span> view;
view<span class="operator">.</span>setSource(<span class="type">QUrl</span>(mainQmlApp));
view<span class="operator">.</span>setResizeMode(<span class="type">QQuickView</span><span class="operator">::</span>SizeRootObjectToView);
<span class="type">QObject</span><span class="operator">::</span>connect(view<span class="operator">.</span>engine()<span class="operator">,</span> SIGNAL(quit())<span class="operator">,</span> qApp<span class="operator">,</span> SLOT(quit()));
view<span class="operator">.</span>setGeometry(<span class="type">QRect</span>(<span class="number">100</span><span class="operator">,</span> <span class="number">100</span><span class="operator">,</span> <span class="number">360</span><span class="operator">,</span> <span class="number">640</span>));
view<span class="operator">.</span>show();
<span class="keyword">return</span> application<span class="operator">.</span>exec();
}</pre>
<p>Finally, in the actual QML, we instantiate the AppModel.</p>
<pre class="qml">import WeatherInfo 1.0
<span class="type">Item</span> {
<span class="name">id</span>: <span class="name">window</span>
<span class="type">AppModel</span> {
<span class="name">id</span>: <span class="name">model</span>
<span class="name">onReadyChanged</span>: {
<span class="keyword">if</span> (<span class="name">model</span>.<span class="name">ready</span>)
<span class="name">window</span>.<span class="name">state</span> <span class="operator">=</span> <span class="string">"ready"</span>
<span class="keyword">else</span>
<span class="name">window</span>.<span class="name">state</span> <span class="operator">=</span> <span class="string">"loading"</span>
}
}
}</pre>
<p>Once instantiated like this, we can use its properties elsewhere in the QML document:</p>
<pre class="qml"> <span class="type">BigForecastIcon</span> {
<span class="name">id</span>: <span class="name">current</span>
<span class="name">width</span>: <span class="name">main</span>.<span class="name">width</span> <span class="operator">-</span><span class="number">12</span>
<span class="name">height</span>: <span class="number">2</span> <span class="operator">*</span> (<span class="name">main</span>.<span class="name">height</span> <span class="operator">-</span> <span class="number">25</span> <span class="operator">-</span> <span class="number">12</span>) <span class="operator">/</span> <span class="number">3</span>
<span class="name">weatherIcon</span>: (<span class="name">model</span>.<span class="name">hasValidWeather</span>
? <span class="name">model</span>.<span class="name">weather</span>.<span class="name">weatherIcon</span>
: <span class="string">"01d"</span>)
}</pre>
<p>Files:</p>
<ul>
<li><a href="qtpositioning-weatherinfo-appmodel-cpp.html">weatherinfo/appmodel.cpp</a></li>
<li><a href="qtpositioning-weatherinfo-appmodel-h.html">weatherinfo/appmodel.h</a></li>
<li><a href="qtpositioning-weatherinfo-weatherinfo-qml.html">weatherinfo/weatherinfo.qml</a></li>
<li><a href="qtpositioning-weatherinfo-components-bigforecasticon-qml.html">weatherinfo/components/BigForecastIcon.qml</a></li>
<li><a href="qtpositioning-weatherinfo-components-forecasticon-qml.html">weatherinfo/components/ForecastIcon.qml</a></li>
<li><a href="qtpositioning-weatherinfo-components-weathericon-qml.html">weatherinfo/components/WeatherIcon.qml</a></li>
<li><a href="qtpositioning-weatherinfo-main-cpp.html">weatherinfo/main.cpp</a></li>
<li><a href="qtpositioning-weatherinfo-weatherinfo-pro.html">weatherinfo/weatherinfo.pro</a></li>
<li><a href="qtpositioning-weatherinfo-weatherinfo-qrc.html">weatherinfo/weatherinfo.qrc</a></li>
</ul>
</div>
<!-- @@@weatherinfo -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2014 Digia Plc and/or its
subsidiaries. Documentation contributions included herein are the copyrights of
their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|