From b508a98e6ae8ff661a6c13c9b87e0d08e04512c4 Mon Sep 17 00:00:00 2001 From: Miikka Heikkinen Date: Fri, 3 Jul 2020 14:22:17 +0300 Subject: [PATCH] QmlDesigner: Fix spinbox empty value handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Henning Gründl Reviewed-by: Thomas Hartmann --- .../imports/StudioControls/RealSpinBox.qml | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/StudioControls/RealSpinBox.qml b/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/StudioControls/RealSpinBox.qml index e0c6a017cd8..85e21735de9 100644 --- a/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/StudioControls/RealSpinBox.qml +++ b/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/StudioControls/RealSpinBox.qml @@ -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)