1

我有一个表视图。名为“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 + ";"
            }

不幸的是,我只得到名为“OrderedID”的第一列的值。

qml: 1;undefined;4;undefined;

非常感谢您的帮助

4

1 回答 1

2

我认为问题是您需要将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())
                        }
                    }
                }
            }
        }
    }
}
于 2016-04-07T08:26:50.440 回答