Forums

OverviewV-Play 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 VPlayApps 1.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 VPlayApps 1.0
    import VPlay 2.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
    V-Play 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.

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support