<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- examples-qmlweather.qdoc --> <title>Qml Weather | Qt Charts 5.11</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td ><a href="../qtdoc/index.html">Qt 5.11</a></td><td ><a href="qtcharts-index.html">Qt Charts</a></td><td ><a href="qtcharts-examples.html">Qt Charts Examples</a></td><td >Qml Weather</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right">Qt 5.11.2 Reference Documentation</td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#running-the-example">Running the Example</a></li> <li class="level1"><a href="#using-charts-in-qt-quick-applications">Using Charts in Qt Quick Applications</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Qml Weather</h1> <span class="subtitle"></span> <!-- $$$qmlweather-brief --> <p>This is a basic demonstration showing how to use the different chart types by using qml.</p> <!-- @@@qmlweather --> <!-- $$$qmlweather-description --> <div class="descr"> <a name="details"></a> <p class="centerAlign"><img src="images/examples_qmlweather.png" alt="" /></p><p>By default the application uses static test data to mimic a weather forecast. You can also obtain an application id from http://www.worldweatheronline.com/ to get access to weather API provided by World Weather Online. You can then give your application id as a parameter to the Qml Weather executable to make it use live data.</p> <p>For example:</p> <pre class="cpp"> bin\qmlweather<span class="operator">.</span>exe <span class="number">1234567890abcdef123456</span> </pre> <a name="running-the-example"></a> <h2 id="running-the-example">Running the Example</h2> <p>To run the example from <a href="http://doc.qt.io/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://doc.qt.io/qtcreator/creator-build-example-application.html">Building and Running an Example</a>.</p> <a name="using-charts-in-qt-quick-applications"></a> <h2 id="using-charts-in-qt-quick-applications">Using Charts in Qt Quick Applications</h2> <p>The example application uses a <a href="qml-qtcharts-chartview.html">ChartView</a> and a some series to visualize weather data:</p> <pre class="qml"> <span class="type"><a href="qml-qtcharts-chartview.html">ChartView</a></span> { <span class="name">id</span>: <span class="name">chartView</span> <span class="name">title</span>: <span class="string">"Weather forecast"</span> <span class="type"><a href="qml-qtcharts-barcategoryaxis.html">BarCategoryAxis</a></span> { <span class="name">id</span>: <span class="name">barCategoriesAxis</span> <span class="name">titleText</span>: <span class="string">"Date"</span> } <span class="type"><a href="qml-qtcharts-valueaxis.html">ValueAxis</a></span>{ <span class="name">id</span>: <span class="name">valueAxisY2</span> <span class="name">min</span>: <span class="number">0</span> <span class="name">max</span>: <span class="number">10</span> <span class="name">titleText</span>: <span class="string">"Rainfall [mm]"</span> } <span class="type"><a href="qml-qtcharts-valueaxis.html">ValueAxis</a></span> { <span class="name">id</span>: <span class="name">valueAxisX</span> <span class="comment">// Hide the value axis; it is only used to map the line series to bar categories axis</span> <span class="name">visible</span>: <span class="number">false</span> <span class="name">min</span>: <span class="number">0</span> <span class="name">max</span>: <span class="number">5</span> } <span class="type"><a href="qml-qtcharts-valueaxis.html">ValueAxis</a></span>{ <span class="name">id</span>: <span class="name">valueAxisY</span> <span class="name">min</span>: <span class="number">0</span> <span class="name">max</span>: <span class="number">15</span> <span class="name">titleText</span>: <span class="string">"Temperature [&deg;C]"</span> } <span class="type"><a href="qml-qtcharts-lineseries.html">LineSeries</a></span> { <span class="name">id</span>: <span class="name">maxTempSeries</span> <span class="name">axisX</span>: <span class="name">valueAxisX</span> <span class="name">axisY</span>: <span class="name">valueAxisY</span> <span class="name">name</span>: <span class="string">"Max. temperature"</span> } <span class="type"><a href="qml-qtcharts-lineseries.html">LineSeries</a></span> { <span class="name">id</span>: <span class="name">minTempSeries</span> <span class="name">axisX</span>: <span class="name">valueAxisX</span> <span class="name">axisY</span>: <span class="name">valueAxisY</span> <span class="name">name</span>: <span class="string">"Min. temperature"</span> } <span class="type"><a href="qml-qtcharts-barseries.html">BarSeries</a></span> { <span class="name">id</span>: <span class="name">myBarSeries</span> <span class="name">axisX</span>: <span class="name">barCategoriesAxis</span> <span class="name">axisYRight</span>: <span class="name">valueAxisY2</span> <span class="type"><a href="qml-qtcharts-barset.html">BarSet</a></span> { <span class="name">id</span>: <span class="name">rainfallSet</span> <span class="name">label</span>: <span class="string">"Rainfall"</span> } } </pre> <p>To get data with weather forecast data, we make an HTTP GET request to World Weather Online. We request the response in JSON data format.</p> <pre class="qml"> <span class="comment">// Make HTTP GET request and parse the result</span> var <span class="name">xhr</span> = new <span class="name">XMLHttpRequest</span>; <span class="name">xhr</span>.<span class="name">open</span>(<span class="string">"GET"</span>, <span class="string">"http://free.worldweatheronline.com/feed/weather.ashx?q=Jyv%c3%a4skyl%c3%a4,Finland&format=json&num_of_days=5&key="</span> <span class="operator">+</span> <span class="name">weatherAppKey</span>); <span class="name">xhr</span>.<span class="name">onreadystatechange</span> <span class="operator">=</span> <span class="keyword">function</span>() { <span class="keyword">if</span> (<span class="name">xhr</span>.<span class="name">readyState</span> <span class="operator">==</span> <span class="name">XMLHttpRequest</span>.<span class="name">DONE</span>) { var <span class="name">a</span> = <span class="name">JSON</span>.<span class="name">parse</span>(<span class="name">xhr</span>.<span class="name">responseText</span>); <span class="name">parseWeatherData</span>(<span class="name">a</span>); } } <span class="name">xhr</span>.<span class="name">send</span>(); </pre> <p>The JSON response contains an array of forecast data:</p> <pre class="qml"> <span class="comment">// Loop through the parsed JSON</span> <span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">i</span> in <span class="name">weatherData</span>.<span class="name">data</span>.<span class="name">weather</span>) { var <span class="name">weatherObj</span> = <span class="name">weatherData</span>.<span class="name">data</span>.<span class="name">weather</span>[<span class="name">i</span>]; </pre> <p>That is then used as input data for our series and a <a href="../qtqml/qml-qtqml-models-listmodel.html">ListModel</a> we use as a container for weather icon URLs:</p> <pre class="qml"> <span class="comment">// Store temperature values, rainfall and weather icon.</span> <span class="comment">// The temperature values begin from 0.5 instead of 0.0 to make the start from the</span> <span class="comment">// middle of the rainfall bars. This makes the temperature lines visually better</span> <span class="comment">// synchronized with the rainfall bars.</span> <span class="name">maxTempSeries</span>.<span class="name">append</span>(<span class="name">Number</span>(<span class="name">i</span>) <span class="operator">+</span> <span class="number">0.5</span>, <span class="name">weatherObj</span>.<span class="name">tempMaxC</span>); <span class="name">minTempSeries</span>.<span class="name">append</span>(<span class="name">Number</span>(<span class="name">i</span>) <span class="operator">+</span> <span class="number">0.5</span>, <span class="name">weatherObj</span>.<span class="name">tempMinC</span>); <span class="name">rainfallSet</span>.<span class="name">append</span>(<span class="name">i</span>, <span class="name">weatherObj</span>.<span class="name">precipMM</span>); <span class="name">weatherImageModel</span>.<span class="name">append</span>({"imageSource":<span class="name">weatherObj</span>.<span class="name">weatherIconUrl</span>[<span class="number">0</span>].<span class="name">value</span>}); </pre> <p>Files:</p> <ul> <li><a href="qtcharts-qmlweather-main-cpp.html">qmlweather/main.cpp</a></li> <li><a href="qtcharts-qmlweather-qml-qmlweather-main-qml.html">qmlweather/qml/qmlweather/main.qml</a></li> <li><a href="qtcharts-qmlweather-qmlweather-pro.html">qmlweather/qmlweather.pro</a></li> <li><a href="qtcharts-qmlweather-resources-qrc.html">qmlweather/resources.qrc</a></li> </ul> </div> <!-- @@@qmlweather --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2018 The Qt Company Ltd. 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/> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>