gpt4 book ai didi

qt - 在 QML TableView 中单击时编辑数据(如 excel)

转载 作者:行者123 更新时间:2023-12-01 14:21:45 25 4
gpt4 key购买 nike

我有一些代码

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一中Connections { target: loaderEditor.item ... } .与 onAccepted处理程序,只有在按下 Enter 键时才会保存更改。

引自 documentation :

accepted()

This signal is emitted when the Return or Enter key is pressed. Note that if there is a validator or inputMask set on the text input, the signal will only be emitted if the input is in an acceptable state.

The corresponding handler is onAccepted. In the original variant changes are saved only



附言需要说明的是,原码可以在 here找到.

关于qt - 在 QML TableView 中单击时编辑数据(如 excel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23856114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com