我有一个TableView。第3列名为" OrderedAmount"包含一个旋转框。
TableView {
id: lw_order
x: 420
y: 63
width: 169
height: 253
model: orderModelId
TableViewColumn
{
id: tableorderid
role: "OrderedID"
title: "ID"
width: 10
}
TableViewColumn
{
role: "OrderedItems"
title: "Items"
width: 100
}
TableViewColumn
{
id: tableamountid
role: "OrderedAmount"
title: "Amount"
width: 50
delegate: Item {
SpinBox {
id: tableamountspin
anchors.verticalCenter: parent.verticalCenter
minimumValue: 1
width: 40
}
}
}
ListModel {
id: orderModelId
}
}
如何从表格的每一行检索SpinBox的实际值?
我试过了:
for (var index = 0 ; index < lw_order.rowCount; index++)
{
order = order + orderModelId.get(index).OrderedID + ";" + orderModelId.get(index).OrderedAmount + ";"
}
不幸的是,我只得到名为&#34; OrderedID&#34;的第一列的值。
qml: 1;undefined;4;undefined;
非常感谢您的帮助
答案 0 :(得分:1)
我认为问题是您需要将SpinBox
值设置为styleData.value
,这是OrderedAmount
元素的值。
如果您这样做,那么您可以使用以下方法在函数中获得正确的值:
orderModelId.get(index).OrderedAmount
请记住,当SpinBox
更改时,您需要更新模型。像这样:
orderModelId.setProperty(styleData.row, "OrderedAmount", value.toString())
考虑到这些想法,代码可能会出现如下:
import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
Window {
visible: true
width: 300
Button {
text: "click me"
function getValues(lw_order, orderModelId) {
for (var index = 0 ; index < lw_order.rowCount; index++)
{
print("row: " + index)
print(" OrderedID: " + orderModelId.get(index).OrderedID + "; " +
" OrderedItems: " + orderModelId.get(index).OrderedItems + "; " +
" OrderedAmount: " + orderModelId.get(index).OrderedAmount)
}
}
onClicked: {
getValues(lw_order, orderModelId)
}
}
ListModel {
id: orderModelId
ListElement {
OrderedID: "1"
OrderedItems: "10"
OrderedAmount: "15"
}
ListElement {
OrderedID: "2"
OrderedItems: "20"
OrderedAmount: "25"
}
ListElement {
OrderedID: "3"
OrderedItems: "30"
OrderedAmount: "35"
}
}
TableView {
id: lw_order
x: 0
y: 50
width: 300
height: 300
model: orderModelId
TableViewColumn
{
id: tableorderid
role: "OrderedID"
title: "ID"
width: 50
}
TableViewColumn
{
role: "OrderedItems"
title: "Items"
width: 100
}
TableViewColumn
{
id: tableamountid
role: "OrderedAmount"
title: "Amount"
width: 70
delegate: Item {
SpinBox {
id: tableamountspin
anchors.verticalCenter: parent.verticalCenter
minimumValue: 1
width: 70
value: styleData.value
onValueChanged: {
if (focus == true) {
console.log("onValueChanged - row: " + styleData.row + " column: " + styleData.column + " value: " + value)
orderModelId.setProperty(styleData.row, "OrderedAmount", value.toString())
}
}
}
}
}
}
}