In questa pagina

Qml Weather

Questa è una dimostrazione di base che illustra come utilizzare i diversi tipi di grafici tramite QML.

Screenshot che mostra tre grafici diversi con tre assi (Temperatura, Data e Precipitazioni): il primo illustra la temperatura massima con un grafico a linee di colore blu, il secondo illustra la temperatura minima, anch’esso con un grafico a linee di colore verde, mentre il terzo illustra le precipitazioni con un grafico a barre di colore arancione

Per impostazione predefinita, l'applicazione utilizza dati di prova statici per simulare una previsione meteorologica. È inoltre possibile ottenere un ID applicazione da http://www.worldweatheronline.com/ per accedere all'API meteorologica fornita da World Weather Online. È quindi possibile specificare l'ID applicazione come parametro dell'eseguibile Qml Weather per far sì che utilizzi dati in tempo reale.

Ad esempio:

bin\qmlweather.exe 1234567890abcdef123456

Esecuzione dell’esempio

È possibile eseguire l’esempio da:

Utilizzo dei grafici nelle applicazioni Qt Quick

L’applicazione di esempio utilizza un ChartView e alcune serie per visualizzare i dati meteorologici:

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"
        }
    }

Per ottenere i dati relativi alle previsioni meteorologiche, inviamo una richiesta HTTP GET a World Weather Online. Richiediamo la risposta in formato JSON.

// 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();

La risposta JSON contiene un array di dati relativi alle previsioni:

// Loop through the parsed JSON
for (var i in weatherData.data.weather) {
    var weatherObj = weatherData.data.weather[i];

Questi dati vengono poi utilizzati come dati di input per le nostre serie e per un ListModel che usiamo come contenitore per gli URL delle icone meteorologiche:

// 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(weatherObj.precipMM);
weatherImageModel.append({"imageSource":weatherObj.weatherIconUrl[0].value});

Progetto di esempio su code.qt.io

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. 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.