summaryrefslogtreecommitdiffstats
path: root/src/gui/util/qvalidator.cpp
diff options
context:
space:
mode:
authorAndy Shaw <andy.shaw@qt.io>2016-03-11 21:27:32 +0100
committerAndy Shaw <andy.shaw@qt.io>2018-04-17 15:36:34 +0000
commit0b70f5ada9aa4e0a44a26f3adb910ac98de31583 (patch)
tree2de9919b9b190a67f5a5d2baf96bf548528817b2 /src/gui/util/qvalidator.cpp
parent49b4433adf254b81f8b7727fbcf88ce2a48100ba (diff)
QDoubleValidator: Allow intermediate values as close as possible to one considered invalid
The documentation states that if you have a range of 0.00-2.00 with the number of decimals set to 2 then any number up to 9.99 would be considered intermediate. This is because the number of digits still matches both before and after the decimal point. If it is 10.0 or 9.999 then it is still considered invalid. In the case of 9.999 being invalid in this case, the documentation is corrected as this was incorrectly indicated as Intermediate, as the code indicates it as Invalid. Change-Id: I07b433e856f355916a1240deafdf4ef58e680639 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/gui/util/qvalidator.cpp')
-rw-r--r--src/gui/util/qvalidator.cpp18
1 files changed, 13 insertions, 5 deletions
diff --git a/src/gui/util/qvalidator.cpp b/src/gui/util/qvalidator.cpp
index 7982ad967e..2d228bf871 100644
--- a/src/gui/util/qvalidator.cpp
+++ b/src/gui/util/qvalidator.cpp
@@ -624,10 +624,10 @@ QDoubleValidator::~QDoubleValidator()
that is within the valid range and is in the correct format.
Returns \l Intermediate if \a input contains a double that is
- outside the range or is in the wrong format; e.g. with too many
- digits after the decimal point or is empty.
+ outside the range or is in the wrong format; e.g. is empty.
- Returns \l Invalid if the \a input is not a double.
+ Returns \l Invalid if the \a input is not a double or with too many
+ digits after the decimal point.
Note: If the valid range consists of just positive doubles (e.g. 0.0 to 100.0)
and \a input is a negative double then \l Invalid is returned. If notation()
@@ -690,8 +690,16 @@ QValidator::State QDoubleValidatorPrivate::validateWithLocale(QString &input, QL
if (notation == QDoubleValidator::StandardNotation) {
double max = qMax(qAbs(q->b), qAbs(q->t));
if (max < LLONG_MAX) {
- qlonglong n = pow10(numDigits(qlonglong(max))) - 1;
- if (qAbs(i) > n)
+ qlonglong n = pow10(numDigits(qlonglong(max)));
+ // In order to get the highest possible number in the intermediate
+ // range we need to get 10 to the power of the number of digits
+ // after the decimal's and subtract that from the top number.
+ //
+ // For example, where q->dec == 2 and with a range of 0.0 - 9.0
+ // then the minimum possible number is 0.00 and the maximum
+ // possible is 9.99. Therefore 9.999 and 10.0 should be seen as
+ // invalid.
+ if (qAbs(i) > (n - std::pow(10, -q->dec)))
return QValidator::Invalid;
}
}