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>
This commit is contained in:
Tuomas Heimonen 2018-06-28 10:49:43 +03:00
parent d5e5e15c1c
commit 4b5afc788f
3 changed files with 22 additions and 12 deletions

View File

@ -411,13 +411,15 @@ QValidator::State QIntValidator::validate(QString & input, int&) const
if (buff.isEmpty()) if (buff.isEmpty())
return Intermediate; return Intermediate;
if (b >= 0 && buff.startsWith('-')) const bool startsWithMinus(buff[0] == '-');
if (b >= 0 && startsWithMinus)
return Invalid; return Invalid;
if (t < 0 && buff.startsWith('+')) const bool startsWithPlus(buff[0] == '+');
if (t < 0 && startsWithPlus)
return Invalid; return Invalid;
if (buff.size() == 1 && (buff.at(0) == '+' || buff.at(0) == '-')) if (buff.size() == 1 && (startsWithPlus || startsWithMinus))
return Intermediate; return Intermediate;
bool ok; bool ok;
@ -433,7 +435,15 @@ QValidator::State QIntValidator::validate(QString & input, int&) const
if (entered >= 0) { if (entered >= 0) {
// the -entered < b condition is necessary to allow people to type // the -entered < b condition is necessary to allow people to type
// the minus last (e.g. for right-to-left languages) // 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 { } else {
return (entered < b) ? Invalid : Intermediate; return (entered < b) ? Invalid : Intermediate;
} }

View File

@ -54,7 +54,7 @@ void tst_QIntValidator::validate_data()
QTest::addColumn<QValidator::State>("state"); QTest::addColumn<QValidator::State>("state");
QTest::newRow("data0") << 0 << 100 << QString("50") << ACC; QTest::newRow("data0") << 0 << 100 << QString("50") << ACC;
QTest::newRow("data1") << 0 << 100 << QString("500") << INV; QTest::newRow("data1") << 0 << 100 << QString("500") << INT;
QTest::newRow("data1a") << 0 << 100 << QString("5000") << INV; QTest::newRow("data1a") << 0 << 100 << QString("5000") << INV;
QTest::newRow("data1b") << -100 << 0 << QString("50") << INT; QTest::newRow("data1b") << -100 << 0 << QString("50") << INT;
QTest::newRow("data1c") << -100 << 0 << QString("500") << INV; QTest::newRow("data1c") << -100 << 0 << QString("500") << INV;
@ -121,7 +121,7 @@ void tst_QIntValidator::validate_data()
QTest::newRow("5.1") << 6 << 8 << QString("5") << INT; QTest::newRow("5.1") << 6 << 8 << QString("5") << INT;
QTest::newRow("5.2") << 6 << 8 << QString("7") << ACC; QTest::newRow("5.2") << 6 << 8 << QString("7") << ACC;
QTest::newRow("5.3") << 6 << 8 << QString("9") << INV; QTest::newRow("5.3") << 6 << 8 << QString("9") << INT;
QTest::newRow("5.3a") << 6 << 8 << QString("19") << INV; QTest::newRow("5.3a") << 6 << 8 << QString("19") << INV;
QTest::newRow("5.4") << -8 << -6 << QString("-5") << INT; QTest::newRow("5.4") << -8 << -6 << QString("-5") << INT;
QTest::newRow("5.5") << -8 << -6 << QString("-7") << ACC; QTest::newRow("5.5") << -8 << -6 << QString("-7") << ACC;
@ -129,13 +129,13 @@ void tst_QIntValidator::validate_data()
QTest::newRow("5.6a") << -8 << -6 << QString("-19") << INV; QTest::newRow("5.6a") << -8 << -6 << QString("-19") << INV;
QTest::newRow("5.7") << -8 << -6 << QString("5") << INT; QTest::newRow("5.7") << -8 << -6 << QString("5") << INT;
QTest::newRow("5.8") << -8 << -6 << QString("7") << INT; QTest::newRow("5.8") << -8 << -6 << QString("7") << INT;
QTest::newRow("5.9") << -8 << -6 << QString("9") << INV; QTest::newRow("5.9") << -8 << -6 << QString("9") << INT;
QTest::newRow("5.10") << -6 << 8 << QString("-5") << ACC; QTest::newRow("5.10") << -6 << 8 << QString("-5") << ACC;
QTest::newRow("5.11") << -6 << 8 << QString("5") << ACC; QTest::newRow("5.11") << -6 << 8 << QString("5") << ACC;
QTest::newRow("5.12") << -6 << 8 << QString("-7") << INV; QTest::newRow("5.12") << -6 << 8 << QString("-7") << INV;
QTest::newRow("5.13") << -6 << 8 << QString("7") << ACC; QTest::newRow("5.13") << -6 << 8 << QString("7") << ACC;
QTest::newRow("5.14") << -6 << 8 << QString("-9") << INV; QTest::newRow("5.14") << -6 << 8 << QString("-9") << INV;
QTest::newRow("5.15") << -6 << 8 << QString("9") << INV; QTest::newRow("5.15") << -6 << 8 << QString("9") << INT;
QTest::newRow("6.1") << 100 << 102 << QString("11") << INT; QTest::newRow("6.1") << 100 << 102 << QString("11") << INT;
QTest::newRow("6.2") << 100 << 102 << QString("-11") << INV; QTest::newRow("6.2") << 100 << 102 << QString("-11") << INV;

View File

@ -2664,9 +2664,9 @@ void tst_QLineEdit::setValidator_QIntValidator_data()
<< 0 << 0
<< 100 << 100
<< QString("153") << QString("153")
<< QString(useKeys ? "15" : "") << QString("153")
<< bool(useKeys) << bool(useKeys)
<< bool(useKeys ? true : false) << bool(false)
<< uint(QLineEdit::Normal); << uint(QLineEdit::Normal);
QTest::newRow(QString(inputMode + "range [-100,100] int '-153'").toLatin1()) QTest::newRow(QString(inputMode + "range [-100,100] int '-153'").toLatin1())
<< -100 << -100
@ -2687,7 +2687,7 @@ void tst_QLineEdit::setValidator_QIntValidator_data()
QTest::newRow(QString(inputMode + "range [3,7] int '8'").toLatin1()) QTest::newRow(QString(inputMode + "range [3,7] int '8'").toLatin1())
<< 3 << 3
<< 7 << 7
<< QString("8") << QString("")
<< QString("") << QString("")
<< bool(useKeys) << bool(useKeys)
<< bool(false) << bool(false)
@ -3292,7 +3292,7 @@ void tst_QLineEdit::editInvalidText()
{ {
QLineEdit *testWidget = ensureTestWidget(); QLineEdit *testWidget = ensureTestWidget();
testWidget->clear(); testWidget->clear();
testWidget->setValidator(new QIntValidator(0, 120, 0)); testWidget->setValidator(new QIntValidator(0, 12, 0));
testWidget->setText("1234"); testWidget->setText("1234");
QVERIFY(!testWidget->hasAcceptableInput()); QVERIFY(!testWidget->hasAcceptableInput());