qt - 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
解决方案
我相信你唯一缺少的是你试图从模型中错误地读取。而不是这个:
temp = inRecentList[i].inningData
做这个:
temp = inRecentList.get(i).inningData
推荐阅读
- azure-functions - 在静态 Web 应用程序的 Azure 函数中实现的 Passport.js 的 CORS 问题。护照返回401
- python - 如何限制多处理模块中 pool.map 的 CPU 使用率?
- node.js - 带有 Socket.io 的 Node.js 在本地工作但不在远程工作(请求 IP 远程更改)
- javascript - fs 节点的来源
- javascript - Sheets API 批量更新特定工作表,原生 Javascript
- c++ - 如何将 UINT16 转换为两个有符号字符?
- javascript - onunload 事件在 window.close 之前触发
- sql-server - 是否可以遍历我的 SQL 数据库中的每个表并找到其他行中未使用的记录?(PK/FK检查)
- php - 内爆函数中的条件
- android - Android Q 和 R 文件权限限制是否适用于 NDK 代码?