Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Forums

OverviewFelgo 2 Support (Qt 5) › My model is not showing any data. Why?

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #18807

    Niyazi

    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

     

     

     

     

    #18816

    Günther
    Felgo Team

    As described in the Qt Docs: http://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html#decorating-views

    ListModel does not require to use e.g.:

    text: modelData.iconText

    when displaying the values. The above statement only works for array-based models.

    With a ListModel, you can directly use

    text: iconText

    in your delegate for showing the list element.

    Also, your singleton model is called: MyModel.qml but you use CbWeatherModel in your code. Please have a look how your singleton component is named exactly.

     

    #18819

    Niyazi

    Yes you are right about singleton. Thank you very much

Viewing 3 posts - 1 through 3 (of 3 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded