4

我有一些代码

import QtQuick 2.2
import QtQuick.Window 2.1
import QtQuick.Controls 1.2

Window {
    visible: true
    width: 538
    height: 360
ToolBar {
    id: toolbar
    width: parent.width

    ListModel {
        id: delegatemenu
        ListElement { text: "Shiny delegate" }
        ListElement { text: "Scale selected" }
        ListElement { text: "Editable items" }
    }

    ComboBox {
        id: delegateChooser
        model: delegatemenu
        width: 150
        anchors.left: parent.left
        anchors.leftMargin: 8
        anchors.verticalCenter: parent.verticalCenter
    }
}

ListModel {
    id: largeModel
    Component.onCompleted: {
        for (var i=0 ; i< 50 ; ++i)
            largeModel.append({"name":"Person "+i , "age": Math.round(Math.random()*100), "gender": Math.random()>0.5 ? "Male" : "Female"})
    }
}


Item {
    anchors.fill: parent

    Component {
        id: editableDelegate
        Item {

            Text {
                width: parent.width
                anchors.margins: 4
                anchors.left: parent.left
                anchors.verticalCenter: parent.verticalCenter
                elide: styleData.elideMode
                text: styleData.value !== undefined ? styleData.value : ""
                color: styleData.textColor
                visible: !styleData.selected
            }
            Loader { 
                id: loaderEditor
                anchors.fill: parent
                anchors.margins: 4
                Connections {
                    target: loaderEditor.item
                    onAccepted: {
                        if (typeof styleData.value === 'number')
                            largeModel.setProperty(styleData.row, styleData.role, Number(parseFloat(loaderEditor.item.text).toFixed(0)))
                        else
                            largeModel.setProperty(styleData.row, styleData.role, loaderEditor.item.text)
                    }
                }
                sourceComponent: styleData.selected ? editor : null
                Component {
                    id: editor
                    TextInput {
                        id: textinput
                        color: styleData.textColor
                        text: styleData.value
                        MouseArea {
                            id: mouseArea
                            anchors.fill: parent
                            hoverEnabled: true
                            onClicked: textinput.forceActiveFocus()
                        }
                    }
                }
            }
        }
    }
    TableView {
        model: largeModel
        anchors.margins: 12
        anchors.fill:parent

        TableViewColumn {
            role: "name"
            title: "Name"
            width: 120
        }
        TableViewColumn {
            role: "age"
            title: "Age"
            width: 120
        }
        TableViewColumn {
            role: "gender"
            title: "Gender"
            width: 120
        }


            itemDelegate: {
                return editableDelegate;
            }
        }
    }
}

为什么当我单击并编辑数据时,有时我的更改没有保存?也许有人对我的问题或代码有解决方案?我只想简单地编辑表格(如 Excel)。谢谢您的回复。

4

2 回答 2

1

onEditingFinished处理程序应该被实现而不是onAccepted一个 in Connections { target: loaderEditor.item ... }。使用onAccepted处理程序,仅当按下 Enter 键时才会保存更改。

从文档中引用:

accepted()

该信号在按下 Return 或 Enter 键时发出。请注意,如果在文本输入上设置了验证器或 inputMask,则仅当输入处于可接受状态时才会发出信号。

相应的处理程序是 onAccepted。在原始变体中,仅保存更改

PS 有必要澄清一下原始代码可以在这里找到。

于 2014-08-31T11:59:29.223 回答
0

@artyom.stv 是对的。我将在这里总结一下:仅使用 onEditingFinished() 并且在此函数中您应该使用 ListModel.set(index, {property: value}) 来实际设置该单元格的值,然后您的更改将被保存。

阅读如何使用 ListModel.set:QML ListModel.set

于 2016-10-12T13:58:38.420 回答