aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndy Shaw <andy.shaw@qt.io>2020-08-18 13:48:50 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2020-09-11 09:01:42 +0000
commit454ca2f441ae3d400255471a7ebe850c6779ad0e (patch)
treecc622395471c0de3d56ffdc2a344c81d12e0fb4f
parent1740afc07950fb0ae3aca0aad2534191805ff672 (diff)
Update the text when the inputted value is out of range
When the inputted value is out of range but it would be fixed to the previous value then it would not update the text correctly to show the corrected value. This ensures that it is updated as appropriate. Before it would check if the value had actually changed after it had been fixed to the corrected value. So if it was corrected to the original value then it would not see it as having changed. Additionally the displayText also has the original text before the change, so we have to force through an update to ensure the contentItem's text is updated too. Change-Id: Ic38787d0803ab59cd998f4e2871c613f1642e764 Fixes: QTBUG-85719 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Mitch Curtis <mitch.curtis@qt.io> (cherry picked from commit 97d2d271233246ed8a0d8930c9110603bf7b03bd) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/quicktemplates2/qquickspinbox.cpp37
-rw-r--r--tests/auto/controls/data/tst_spinbox.qml25
2 files changed, 49 insertions, 13 deletions
diff --git a/src/quicktemplates2/qquickspinbox.cpp b/src/quicktemplates2/qquickspinbox.cpp
index cfd75104..f04ceff8 100644
--- a/src/quicktemplates2/qquickspinbox.cpp
+++ b/src/quicktemplates2/qquickspinbox.cpp
@@ -117,8 +117,8 @@ public:
int effectiveStepSize() const;
- void updateDisplayText();
- void setDisplayText(const QString &displayText);
+ void updateDisplayText(bool modified = false);
+ void setDisplayText(const QString &displayText, bool modified = false);
bool upEnabled() const;
void updateUpEnabled();
@@ -209,24 +209,34 @@ void QQuickSpinBoxPrivate::updateValue()
}
}
+// modified indicates if the value was modified by the user and not programatically
+// this is then passed on to updateDisplayText to indicate that the user has modified
+// the value so it may need to trigger an update of the contentItem's text too
+
bool QQuickSpinBoxPrivate::setValue(int newValue, bool allowWrap, bool modified)
{
Q_Q(QQuickSpinBox);
+ int correctedValue = newValue;
if (q->isComponentComplete())
- newValue = boundValue(newValue, allowWrap);
+ correctedValue = boundValue(newValue, allowWrap);
- if (value == newValue)
+ if (!modified && newValue == correctedValue && newValue == value)
return false;
- value = newValue;
+ const bool emitSignals = (value != correctedValue);
+ value = correctedValue;
- updateDisplayText();
+ updateDisplayText(modified);
updateUpEnabled();
updateDownEnabled();
- emit q->valueChanged();
- if (modified)
- emit q->valueModified();
+ // Only emit the signals if the corrected value is not the same as the
+ // original value to avoid unnecessary updates
+ if (emitSignals) {
+ emit q->valueChanged();
+ if (modified)
+ emit q->valueModified();
+ }
return true;
}
@@ -250,7 +260,7 @@ int QQuickSpinBoxPrivate::effectiveStepSize() const
return from > to ? -1 * stepSize : stepSize;
}
-void QQuickSpinBoxPrivate::updateDisplayText()
+void QQuickSpinBoxPrivate::updateDisplayText(bool modified)
{
Q_Q(QQuickSpinBox);
QString text;
@@ -262,13 +272,14 @@ void QQuickSpinBoxPrivate::updateDisplayText()
} else {
text = locale.toString(value);
}
- setDisplayText(text);
+ setDisplayText(text, modified);
}
-void QQuickSpinBoxPrivate::setDisplayText(const QString &text)
+void QQuickSpinBoxPrivate::setDisplayText(const QString &text, bool modified)
{
Q_Q(QQuickSpinBox);
- if (displayText == text)
+
+ if (!modified && displayText == text)
return;
displayText = text;
diff --git a/tests/auto/controls/data/tst_spinbox.qml b/tests/auto/controls/data/tst_spinbox.qml
index 4ff80686..adb70a30 100644
--- a/tests/auto/controls/data/tst_spinbox.qml
+++ b/tests/auto/controls/data/tst_spinbox.qml
@@ -686,4 +686,29 @@ TestCase {
verify(control)
compare(control.up.indicator.s, "this is the one");
}
+
+ function test_valueEnterFromOutsideRange() {
+ // Check that changing from 2 to 99 goes to 98 then changing to 99 puts it back to 98
+ var control = createTemporaryObject(spinBox, testCase, {from: 2, to: 98, value: 2, editable: true})
+ verify(control)
+
+ control.forceActiveFocus()
+ verify(control.activeFocus)
+
+ keyClick(Qt.Key_Backspace)
+ keyClick(Qt.Key_Backspace)
+ keyClick(Qt.Key_9)
+ keyClick(Qt.Key_9)
+ keyClick(Qt.Key_Return)
+ compare(control.value, 98)
+ compare(control.displayText, "98")
+ compare(control.contentItem.text, "98")
+
+ keyClick(Qt.Key_Backspace)
+ keyClick(Qt.Key_9)
+ keyClick(Qt.Key_Return)
+ compare(control.value, 98)
+ compare(control.displayText, "98")
+ compare(control.contentItem.text, "98")
+ }
}