diff options
author | Tuomas Heimonen <tuomas.heimonen@qt.io> | 2018-06-28 10:49:43 +0300 |
---|---|---|
committer | Tuomas Heimonen <tuomas.heimonen@qt.io> | 2018-06-29 09:17:05 +0000 |
commit | 4b5afc788f8f476a7bb2f57872ad8a017bf0c870 (patch) | |
tree | 59a93a20b954332c13b594023d594a2bb9dac149 /src/gui/util/qvalidator.cpp | |
parent | d5e5e15c1c817cbfa5da8f80f6fa0e08b913e82e (diff) |
QIntValidator: Intermediate for number if digits equal or less than max
Input value which is over the highest acceptable value, but consisting
of a number of digits equal to or less than the max value should be
considered intermediate.
[ChangeLog][QtGui][QIntValidator] Input value with over the highest
acceptable value, but with equal or less amount of digits than the maximum
value is now considered intermediate.
Task-number: QTBUG-59650
Change-Id: I71a77c9c266f0f3b62c71ac6cb995019385c1cf5
Reviewed-by: Andy Shaw <andy.shaw@qt.io>
Diffstat (limited to 'src/gui/util/qvalidator.cpp')
-rw-r--r-- | src/gui/util/qvalidator.cpp | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/src/gui/util/qvalidator.cpp b/src/gui/util/qvalidator.cpp index 2d228bf871..ae4f56a12e 100644 --- a/src/gui/util/qvalidator.cpp +++ b/src/gui/util/qvalidator.cpp @@ -411,13 +411,15 @@ QValidator::State QIntValidator::validate(QString & input, int&) const if (buff.isEmpty()) return Intermediate; - if (b >= 0 && buff.startsWith('-')) + const bool startsWithMinus(buff[0] == '-'); + if (b >= 0 && startsWithMinus) return Invalid; - if (t < 0 && buff.startsWith('+')) + const bool startsWithPlus(buff[0] == '+'); + if (t < 0 && startsWithPlus) return Invalid; - if (buff.size() == 1 && (buff.at(0) == '+' || buff.at(0) == '-')) + if (buff.size() == 1 && (startsWithPlus || startsWithMinus)) return Intermediate; bool ok; @@ -433,7 +435,15 @@ QValidator::State QIntValidator::validate(QString & input, int&) const if (entered >= 0) { // the -entered < b condition is necessary to allow people to type // the minus last (e.g. for right-to-left languages) - return (entered > t && -entered < b) ? Invalid : Intermediate; + // The buffLength > tLength condition validates values consisting + // of a number of digits equal to or less than the max value as intermediate. + + int buffLength = buff.size(); + if (startsWithPlus) + buffLength--; + const int tLength = t != 0 ? static_cast<int>(std::log10(qAbs(t))) + 1 : 1; + + return (entered > t && -entered < b && buffLength > tLength) ? Invalid : Intermediate; } else { return (entered < b) ? Invalid : Intermediate; } |