我有一些代码
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)。感谢您的回复。
onEditingFinished
应该实现处理程序而不是onAccepted
one in Connections { target: loaderEditor.item ... }
. With onAccepted
处理程序,仅当按下 Enter 键时才会保存更改。
引用自文档:
accepted()
当按下 Return 或 Enter 键时会发出此信号。笔记
如果在文本输入上设置了验证器或 inputMask,则
仅当输入处于可接受的状态时才会发出信号。
相应的处理程序是 onAccepted。在原始版本中
仅保存更改
附:需要澄清一下,原来的代码可以找到here.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)