<?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.12.6</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 >Qt 5.12</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"><a href="qtcharts-index.html">Qt Charts | Commercial or GPLv3</a></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 Qt Creator, open the <b>Welcome</b> mode and select the example from <b>Examples</b>. For more information, visit Building and Running an Example.</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"> ChartView { id: chartView title: "Weather forecast" BarCategoryAxis { id: barCategoriesAxis titleText: "Date" } ValueAxis{ id: valueAxisY2 min: 0 max: 10 titleText: "Rainfall [mm]" } ValueAxis { id: valueAxisX // Hide the value axis; it is only used to map the line series to bar categories axis visible: false min: 0 max: 5 } ValueAxis{ id: valueAxisY min: 0 max: 15 titleText: "Temperature [°C]" } LineSeries { id: maxTempSeries axisX: valueAxisX axisY: valueAxisY name: "Max. temperature" } LineSeries { id: minTempSeries axisX: valueAxisX axisY: valueAxisY name: "Min. temperature" } BarSeries { id: myBarSeries axisX: barCategoriesAxis axisYRight: valueAxisY2 BarSet { id: rainfallSet label: "Rainfall" } } </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"> // Make HTTP GET request and parse the result var xhr = new XMLHttpRequest; xhr.open("GET", "http://free.worldweatheronline.com/feed/weather.ashx?q=Jyv%c3%a4skyl%c3%a4,Finland&format=json&num_of_days=5&key=" + weatherAppKey); xhr.onreadystatechange = function() { if (xhr.readyState == XMLHttpRequest.DONE) { var a = JSON.parse(xhr.responseText); parseWeatherData(a); } } xhr.send(); </pre> <p>The JSON response contains an array of forecast data:</p> <pre class="qml"> // Loop through the parsed JSON for (var i in weatherData.data.weather) { var weatherObj = weatherData.data.weather[i]; </pre> <p>That is then used as input data for our series and a ListModel we use as a container for weather icon URLs:</p> <pre class="qml"> // Store temperature values, rainfall and weather icon. // The temperature values begin from 0.5 instead of 0.0 to make the start from the // middle of the rainfall bars. This makes the temperature lines visually better // synchronized with the rainfall bars. maxTempSeries.append(Number(i) + 0.5, weatherObj.tempMaxC); minTempSeries.append(Number(i) + 0.5, weatherObj.tempMinC); rainfallSet.append(i, weatherObj.precipMM); weatherImageModel.append({"imageSource":weatherObj.weatherIconUrl[0].value}); </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> 2019 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>