<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>main.qml Example File | Qt Charts 5.9</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.9</td><td ><a href="qtcharts-index.html">Qt Charts</a></td><td ><a href="qtcharts-qmlweather-example.html">Qml Weather</a></td><td >main.qml Example File</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">main.qml Example File</h1> <span class="subtitle">qmlweather/qml/qmlweather/main.qml</span> <!-- $$$qmlweather/qml/qmlweather/main.qml-description --> <div class="descr"> <a name="details"></a> <pre class="qml"> <span class="comment">/**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Charts module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:GPL$ ** 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 General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3 or (at your option) any later version ** approved by the KDE Free Qt Foundation. The licenses are as published by ** the Free Software Foundation and appearing in the file LICENSE.GPL3 ** included in the packaging of this file. Please review the following ** information to ensure the GNU General Public License requirements will ** be met: https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** ****************************************************************************/</span> import QtQuick 2.0 import QtCharts 2.0 <span class="type">Rectangle</span> { <span class="name">width</span>: <span class="number">500</span> <span class="name">height</span>: <span class="number">400</span> <span class="name">gradient</span>: <span class="name">Gradient</span> { <span class="type">GradientStop</span> { <span class="name">position</span>: <span class="number">0.0</span>; <span class="name">color</span>: <span class="string">"lightblue"</span> } <span class="type">GradientStop</span> { <span class="name">position</span>: <span class="number">1.0</span>; <span class="name">color</span>: <span class="string">"white"</span> } } <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="name">height</span>: <span class="name">parent</span>.<span class="name">height</span> <span class="operator">/</span> <span class="number">4</span> <span class="operator">*</span> <span class="number">3</span> <span class="name">anchors</span>.top: <span class="name">parent</span>.<span class="name">top</span> <span class="name">anchors</span>.left: <span class="name">parent</span>.<span class="name">left</span> <span class="name">anchors</span>.right: <span class="name">parent</span>.<span class="name">right</span> <span class="name">legend</span>.alignment: <span class="name">Qt</span>.<span class="name">AlignTop</span> <span class="name">antialiasing</span>: <span class="number">true</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> } } } <span class="comment">// A timer to refresh the forecast every 5 minutes</span> <span class="type">Timer</span> { <span class="name">interval</span>: <span class="number">300000</span> <span class="name">repeat</span>: <span class="number">true</span> <span class="name">triggeredOnStart</span>: <span class="number">true</span> <span class="name">running</span>: <span class="number">true</span> <span class="name">onTriggered</span>: { <span class="keyword">if</span> (<span class="name">weatherAppKey</span> <span class="operator">!=</span> <span class="string">""</span>) { <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>(); } <span class="keyword">else</span> { <span class="comment">// No app key for worldweatheronline.com given by the user -> use dummy static data</span> var <span class="name">responseText</span> = <span class="string">"{ \"data\": { \"current_condition\": [ {\"cloudcover\": \"10\", \"humidity\": \"61\", \"observation_time\": \"06:26 AM\", \"precipMM\": \"0.0\", \"pressure\": \"1022\", \"temp_C\": \"6\", \"temp_F\": \"43\", \"visibility\": \"10\", \"weatherCode\": \"113\", \"weatherDesc\": [ {\"value\": \"Sunny\" } ], \"weatherIconUrl\": [ {\"value\": \"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png\" } ], \"winddir16Point\": \"SE\", \"winddirDegree\": \"140\", \"windspeedKmph\": \"7\", \"windspeedMiles\": \"4\" } ], \"request\": [ {\"query\": \"Jyvaskyla, Finland\", \"type\": \"City\" } ], \"weather\": [ {\"date\": \"2012-05-09\", \"precipMM\": \"0.4\", \"tempMaxC\": \"14\", \"tempMaxF\": \"57\", \"tempMinC\": \"7\", \"tempMinF\": \"45\", \"weatherCode\": \"116\", \"weatherDesc\": [ {\"value\": \"Partly Cloudy\" } ], \"weatherIconUrl\": [ {\"value\": \"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png\" } ], \"winddir16Point\": \"S\", \"winddirDegree\": \"179\", \"winddirection\": \"S\", \"windspeedKmph\": \"20\", \"windspeedMiles\": \"12\" }, {\"date\": \"2012-05-10\", \"precipMM\": \"2.4\", \"tempMaxC\": \"13\", \"tempMaxF\": \"55\", \"tempMinC\": \"8\", \"tempMinF\": \"46\", \"weatherCode\": \"266\", \"weatherDesc\": [ {\"value\": \"Light drizzle\" } ], \"weatherIconUrl\": [ {\"value\": \"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0017_cloudy_with_light_rain.png\" } ], \"winddir16Point\": \"SW\", \"winddirDegree\": \"219\", \"winddirection\": \"SW\", \"windspeedKmph\": \"21\", \"windspeedMiles\": \"13\" }, {\"date\": \"2012-05-11\", \"precipMM\": \"11.1\", \"tempMaxC\": \"15\", \"tempMaxF\": \"59\", \"tempMinC\": \"7\", \"tempMinF\": \"44\", \"weatherCode\": \"266\", \"weatherDesc\": [ {\"value\": \"Light drizzle\" } ], \"weatherIconUrl\": [ {\"value\": \"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0017_cloudy_with_light_rain.png\" } ], \"winddir16Point\": \"SSW\", \"winddirDegree\": \"200\", \"winddirection\": \"SSW\", \"windspeedKmph\": \"20\", \"windspeedMiles\": \"12\" }, {\"date\": \"2012-05-12\", \"precipMM\": \"2.8\", \"tempMaxC\": \"7\", \"tempMaxF\": \"44\", \"tempMinC\": \"2\", \"tempMinF\": \"35\", \"weatherCode\": \"317\", \"weatherDesc\": [ {\"value\": \"Light sleet\" } ], \"weatherIconUrl\": [ {\"value\": \"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0021_cloudy_with_sleet.png\" } ], \"winddir16Point\": \"NW\", \"winddirDegree\": \"311\", \"winddirection\": \"NW\", \"windspeedKmph\": \"24\", \"windspeedMiles\": \"15\" }, {\"date\": \"2012-05-13\", \"precipMM\": \"0.4\", \"tempMaxC\": \"6\", \"tempMaxF\": \"42\", \"tempMinC\": \"2\", \"tempMinF\": \"35\", \"weatherCode\": \"116\", \"weatherDesc\": [ {\"value\": \"Partly Cloudy\" } ], \"weatherIconUrl\": [ {\"value\": \"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png\" } ], \"winddir16Point\": \"WNW\", \"winddirDegree\": \"281\", \"winddirection\": \"WNW\", \"windspeedKmph\": \"21\", \"windspeedMiles\": \"13\" } ] }}"</span>; var <span class="name">a</span> = <span class="name">JSON</span>.<span class="name">parse</span>(<span class="name">responseText</span>); <span class="name">parseWeatherData</span>(<span class="name">a</span>); } } } <span class="type">Row</span> { <span class="name">id</span>: <span class="name">weatherImageRow</span> <span class="name">anchors</span>.top: <span class="name">chartView</span>.<span class="name">bottom</span> <span class="name">anchors</span>.topMargin: <span class="number">5</span> <span class="name">anchors</span>.bottom: <span class="name">poweredByText</span>.<span class="name">top</span> <span class="name">anchors</span>.bottomMargin: <span class="number">5</span> <span class="name">anchors</span>.horizontalCenter: <span class="name">parent</span>.<span class="name">horizontalCenter</span> <span class="name">height</span>: <span class="name">parent</span>.<span class="name">height</span> <span class="operator">-</span> <span class="name">chartView</span>.<span class="name">height</span> <span class="operator">-</span> <span class="name">anchors</span>.<span class="name">topMargin</span> <span class="type">ListModel</span> { <span class="name">id</span>: <span class="name">weatherImageModel</span> } <span class="type">Repeater</span> { <span class="name">id</span>: <span class="name">repeater</span> <span class="name">model</span>: <span class="name">weatherImageModel</span> <span class="name">delegate</span>: <span class="name">Image</span> { <span class="name">source</span>: <span class="name">imageSource</span> <span class="name">width</span>: <span class="name">weatherImageRow</span>.<span class="name">height</span> <span class="name">height</span>: <span class="name">width</span> <span class="name">fillMode</span>: <span class="name">Image</span>.<span class="name">PreserveAspectCrop</span> } } } <span class="type">Text</span> { <span class="name">id</span>: <span class="name">poweredByText</span> <span class="name">anchors</span>.bottom: <span class="name">parent</span>.<span class="name">bottom</span> <span class="name">anchors</span>.bottomMargin: <span class="number">5</span> <span class="name">anchors</span>.left: <span class="name">parent</span>.<span class="name">left</span> <span class="name">anchors</span>.leftMargin: <span class="number">25</span> <span class="name">height</span>: <span class="name">parent</span>.<span class="name">height</span> <span class="operator">/</span> <span class="number">25</span> <span class="name">text</span>: <span class="string">"Powered by World Weather Online"</span> } <span class="keyword">function</span> <span class="name">parseWeatherData</span>(<span class="name">weatherData</span>) { <span class="comment">// Clear previous values</span> <span class="name">maxTempSeries</span>.<span class="name">clear</span>(); <span class="name">minTempSeries</span>.<span class="name">clear</span>(); <span class="name">weatherImageModel</span>.<span class="name">clear</span>(); <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>]; <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>}); <span class="comment">// Update scale of the chart</span> <span class="name">valueAxisY</span>.<span class="name">max</span> <span class="operator">=</span> <span class="name">Math</span>.<span class="name">max</span>(<span class="name">chartView</span>.<span class="name">axisY</span>().<span class="name">max</span>,<span class="name">weatherObj</span>.<span class="name">tempMaxC</span>); <span class="name">valueAxisX</span>.<span class="name">min</span> <span class="operator">=</span> <span class="number">0</span>; <span class="name">valueAxisX</span>.<span class="name">max</span> <span class="operator">=</span> <span class="name">Number</span>(<span class="name">i</span>) <span class="operator">+</span> <span class="number">1</span>; <span class="comment">// Set the x-axis labels to the dates of the forecast</span> var <span class="name">xLabels</span> = <span class="name">barCategoriesAxis</span>.<span class="name">categories</span>; <span class="name">xLabels</span>[<span class="name">Number</span>(<span class="name">i</span>)] <span class="operator">=</span> <span class="name">weatherObj</span>.<span class="name">date</span>.<span class="name">substring</span>(<span class="number">5</span>, <span class="number">10</span>); <span class="name">barCategoriesAxis</span>.<span class="name">categories</span> <span class="operator">=</span> <span class="name">xLabels</span>; <span class="name">barCategoriesAxis</span>.<span class="name">visible</span> <span class="operator">=</span> <span class="number">true</span>; <span class="name">barCategoriesAxis</span>.<span class="name">min</span> <span class="operator">=</span> <span class="number">0</span>; <span class="name">barCategoriesAxis</span>.<span class="name">max</span> <span class="operator">=</span> <span class="name">xLabels</span>.<span class="name">length</span> <span class="operator">-</span> <span class="number">1</span>; } } } </pre> </div> <!-- @@@qmlweather/qml/qmlweather/main.qml --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2017 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>