首页 > 解决方案 > Can't create an object on the QML list

问题描述

Can't create an object on the list.

I need the recent requests to be displayed in the "Recent" list. After submitting, I store the request in the listOfRecents array.

The problem is that I cannot create a list object by taking data from that array. Below are the pieces of code and the error.

Here's the code with the list:

Page {
    id: serv
    title: qsTr("Recent")

    function addRecent()
    {
        inRecentList.clear()
        for(var i = 0; i < listOfRecents.length; ++i)
        {
            var temp = listOfRecents[i];
            inRecentList.append({inningData: listOfRecents[i],
                                 inningShow: listOfRecents[i]})
            temp = inRecentList[i].inningData
            temp = ""
        }
    }

    Component.onCompleted: {
        addRecent()
    }

    ListView {
        id: inRecent

        x: 5
        y: 5

        width: parent.width - 10
        height: parent.height - 50
        spacing: 2


        delegate: RecentItem {
            isData: inningData
            isShow: inningShow
        }

        model: ListModel {
            id: inRecentList
        }
    }
}

Here is the RecentItem code:

Item {
id: inning

property string isShow: ""
property string isData: ""

height: 32
width: inRecent.width

Button
{
    height: parent.height
    width: parent.width

    Row {
        anchors.fill: parent

        spacing: 10

        Image {
            id: img
            source: "Res/images/ui_elements/query.png"
        }

        Text {
            text: isShow

            font{
                bold: true
                italic: true

                pixelSize: 24
            }
        }
    }

        onClicked: {
            stackView.pop()
            stackView.pop()
            sTextToRecent(isData)
        }
    }
}

It gives the following error in the console:

qrc:/Recent.qml:19: TypeError: Cannot read property 'inningData' of undefined
qrc:/Recent.qml:41: ReferenceError: inningShow is not defined
qrc:/Recent.qml:40: ReferenceError: inningData is not defined

标签: qtqml

解决方案


我相信你唯一缺少的是你试图从模型中错误地读取。而不是这个:

temp = inRecentList[i].inningData

做这个:

temp = inRecentList.get(i).inningData

推荐阅读