我希望FloatTextField
的用法像这样:
Components.FloatTextField {
value: OffsetRole / 1000.0
onValueChanged: core.dataStorage().recording(IdRole).offset = value * 1000.0
}
但是目前它是这样工作的。请注意,value和internalValue都是公开的。
Components.FloatTextField {
value: OffsetRole / 1000.0
onLocalValueChanged: core.dataStorage().recording(IdRole).offset = localValue * 1000.0
}
这是我实现FloatTextField的方法:
Components.TextField {
Component.onCompleted: updateText(value)
}
onValueChanged: {
if (localValue !== value) {
localValue = value
updateText(value)
}
}
onEditingFinished: {
updateLocalValue(text)
updateText(localValue)
}
function toLocale(text) {
return text.replace(",", settings.separator).replace(".", settings.separator)
}
function updateText(num) {
var newText
if (decimals == -1 || num === 0) {
newText = toLocale(num.toString())
} else {
newText = toLocale(num.toFixed(decimals))
}
text = newText
}
function updateLocalValue(text) {
var newText = toLocale(text)
if (newText.charAt(0) === settings.separator) {
newText = "0" + newText
}
if (newText.charAt(0) === "-" && newText.charAt(1) === settings.separator) {
newText = "-0" + newText.substr(1, newText.length)
}
if (newText.search(/\d/) === -1) {
newText = "0"
}
var num = Number.fromLocaleString(newText)
if (isNaN(num)) {
num = 0
}
if (num > maximumValue) {
num = maximumValue
} else if (num < minimumValue) {
num = minimumValue
}
if (num === -0) {
num = 0
}
localValue = num
}
除了我的主要问题外,我想听听您对如何改善数据绑定的任何建议。上述解决方案是本地开发的,可能不是最佳实践。