QmlDesigner: Fix spinbox empty value handling

Force zero value when text value converts to NaN.

Also, onEditingFinished doesn't trigger if value is something not
accepted by the validator (the empty value), so do the equivalent
also on focus loss, if value is still dirty (meaning onEditingFinished
didn't happen). This ensures we're never left with different value
shown and written in qml file.

Change-Id: I3862d8d11adf7955f99b50b515e015ad132a5c78
Fixes: QDS-1539
Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
Reviewed-by: Henning Gründl <henning.gruendl@qt.io>
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Miikka Heikkinen
2020-07-03 14:22:17 +03:00
committed by Mahmoud Badri
parent 3f655815be
commit b508a98e6a

View File

@@ -149,7 +149,7 @@ T.SpinBox {
myControl: mySpinBox
validator: doubleValidator
onEditingFinished: {
function handleEditingFinished() {
// Keep the dirty state before calling setValueFromInput(),
// it will be set to false (cleared) internally
var valueModified = mySpinBox.dirty
@@ -161,6 +161,8 @@ T.SpinBox {
if (valueModified)
mySpinBox.compressedRealValueModified()
}
onEditingFinished: handleEditingFinished()
onTextEdited: mySpinBox.dirty = true
}
@@ -278,8 +280,16 @@ T.SpinBox {
}
onRealValueModified: myTimer.restart()
onFocusChanged: {
if (mySpinBox.focus)
if (mySpinBox.focus) {
mySpinBox.dirty = false
} else {
// Make sure displayed value is correct after focus loss, as onEditingFinished
// doesn't trigger when value is something validator doesn't accept.
spinBoxInput.text = mySpinBox.textFromValue(mySpinBox.realValue, mySpinBox.locale)
if (mySpinBox.dirty)
spinBoxInput.handleEditingFinished()
}
}
onDisplayTextChanged: spinBoxInput.text = mySpinBox.displayText
onActiveFocusChanged: {
@@ -348,6 +358,9 @@ T.SpinBox {
}
function setRealValue(value) {
if (Number.isNaN(value))
value = 0
if (mySpinBox.decimals === 0)
value = Math.round(value)