Why is my model is not showing any data.
The “day”: “temperature”: and “iconText”: not showing anything. And I am getting error as:
WeatherWidget.qml:103: <b>TypeError: Cannot call method ‘append’ of undefined</b>
I have to declare a ListModel in singletons/MyModel.qml as;
———————————————————————————
pragma Singleton
import Felgo 3.0
import QtQuick 2.9
Item {
//weather model
property ListModel weatherModel: ListModel {
// 1.Day
ListElement {
day: ""
temperature: ""
iconText: ""
}
// 2.Day
ListElement {
day: ""
temperature: ""
iconText: ""
}
// 3.Day
ListElement {
day: ""
temperature: ""
iconText: ""
}
}
}
And I want to use my ListModel as:
mainui/WeatherWidget.qml
———————————————————————————
import Felgo 3.0
import QtQuick 2.9
import QtQuick.Layouts 1.3
import "../singletons"
Rectangle {
id: rectTopWeather
anchors.fill: parent
color: "#ffffff"
radius: 10
opacity: 0.1
AppListView {
id: weatherView
anchors.fill: parent
orientation: ListView.Horizontal
model: CbWeatherModel.weatherModel
delegate: Item {
width: Math.max(110, rectTopWeather.width / weatherView.count)
height: rectTopWeather.height
// Content
ColumnLayout {
anchors.fill: parent
anchors.topMargin: 10
anchors.bottomMargin: 10
// day
Text {
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
text: model.day
}
// icon
Image {
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
source: model.icon
}
// temperature
Text {
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
text: "%1ËšC".arg(model.temperature)
}
// seperator
Rectangle {
anchors.verticalCenter: parent.verticalCenter
anchors.right: parent.right
width: 1
height: (rectTopWeather.height / 3) * 2
color: "lightgrey"
visible: model.index < weatherView.count - 1
}
} // Content
} // delegate: Item
} // weatherView
} // rectTopWeather
// Component on Complete
Component.onCompleted: {
getJsonData()
console.log("On Completed")
}
// getJsonData
function getJsonData() {
var request = new XMLHttpRequest()
request.open('GET', "https://****************”, true);
request.onreadystatechange = function() {
if (request.readyState === XMLHttpRequest.DONE) {
if (request.status && request.status === 200) {
var result = JSON.parse(request.responseText)
sortJsonData(result)
}
else {
console.log("WeatherLog:", request.status, request.statusText)
}
}
}
request.send()
} // getJsonData
function sortJsonData(result) {
var i = 0;
for (i = 0; i < 3; i++) {
// Get Today Weather Result
if (i === 0) {
var getStatusIcon = ""
getStatusIcon = result.query.results.channel.item.forecast[i].code
CbWeatherModel.weatherModel.append(i, { "day": result.query.results.channel.item.forecast[i].day,
"temperature": result.query.results.channel.item.forecast[i].high,
"iconText": "../../assets/weather/" + getStatusIcon + ".png"
})
} // for loop
console.log("Weather Refreshed")
} // function sortJsonData()
} // function sortJsonData
} // myWeatherItem