【发布时间】:2013-01-20 23:13:42
【问题描述】:
我需要一些帮助来将元素添加到 qml 列表视图中,我有一个 textarea 和一个按钮,当按下时会将 textarea 文本添加到列表视图项中,这是我的尝试:
Component {
id: delegate
Item {
width: 200; height: 28
Label {
text: score
}
}
}
ListView {
id: p1scores
model: p1model
delegate: delegate
anchors.top: p1name.bottom
anchors.topMargin: units.gu(1)
}
ListModel {
id: p1model
ListElement { score: "0" }
}
TextArea {
id: p1input
width: units.gu(8)
height: units.gu(3)
horizontalAlignment: TextEdit.AlignHCenter
inputMethodHints: Qt.ImhDigitsOnly
contentHeight: units.gu(60)
anchors.topMargin: units.gu(8)
}
Button {
id:p1button
text: i18n.tr("Add")
width: units.gu(8)
onClicked: {
p1model.append({"score": p1input.text})
p1input.text = ""
}
}
我尝试添加它,但没有显示在列表视图中...有什么帮助吗?
【问题讨论】:
-
在您的 p1button onClicked 中,您在 将文本添加到 ListModel 之前清除了 TextArea 中的文本,当然文本不会显示出来。
-
文本仍然没有显示,我认为这是代表的问题...(编辑问题)
-
我不确定但是...将您的
delegate组件 ID 更改为其他内容,因为当您在 ListView 中使用delegate: delegate时,它假定为delegate: p1scores.delegate,所以它绑定回来对自己。 -
我发现了问题,是我没有指定列表视图的高度,显然默认是单个项目的大小...