From 445ef8847979dab72893aab1924d46d0fe1a8a3e Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Tue, 2 Nov 2010 17:25:54 +0100 Subject: With some locales, QDoubleValidator would not accept "C" locale valid numbers Locales using dot as thousands delimiter and comma as decimal separator are prone to this error. This is a regression introduced by commit b81b8e43ad57183ed66. Auto-tests included. Reviewed-by: Olivier Task-number: QTBUG_14935 --- src/corelib/tools/qlocale.h | 2 +- src/gui/widgets/qvalidator.cpp | 45 +++++++++++++--------- .../auto/qdoublevalidator/tst_qdoublevalidator.cpp | 4 ++ 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/corelib/tools/qlocale.h b/src/corelib/tools/qlocale.h index 8b424bb..1af2cb4 100644 --- a/src/corelib/tools/qlocale.h +++ b/src/corelib/tools/qlocale.h @@ -114,7 +114,7 @@ class Q_CORE_EXPORT QLocale friend class QString; friend class QByteArray; friend class QIntValidator; - friend class QDoubleValidator; + friend class QDoubleValidatorPrivate; friend class QTextStream; friend class QTextStreamPrivate; diff --git a/src/gui/widgets/qvalidator.cpp b/src/gui/widgets/qvalidator.cpp index b75db45..130d091 100644 --- a/src/gui/widgets/qvalidator.cpp +++ b/src/gui/widgets/qvalidator.cpp @@ -499,6 +499,8 @@ public: } QDoubleValidator::Notation notation; + + QValidator::State validateWithLocale(QString & input, QLocalePrivate::NumberMode numMode, const QLocale &locale) const; }; @@ -654,42 +656,49 @@ QValidator::State QDoubleValidator::validate(QString & input, int &) const break; } + State currentLocaleValidation = d->validateWithLocale(input, numMode, locale()); + if (currentLocaleValidation == Acceptable || locale().language() == QLocale::C) + return currentLocaleValidation; + State cLocaleValidation = d->validateWithLocale(input, numMode, QLocale(QLocale::C)); + return qMax(currentLocaleValidation, cLocaleValidation); +} + +QValidator::State QDoubleValidatorPrivate::validateWithLocale(QString &input, QLocalePrivate::NumberMode numMode, const QLocale &locale) const +{ + Q_Q(const QDoubleValidator); QByteArray buff; - if (!locale().d()->validateChars(input, numMode, &buff, dec)) { - QLocale cl(QLocale::C); - if (!cl.d()->validateChars(input, numMode, &buff, dec)) - return Invalid; - } + if (!locale.d()->validateChars(input, numMode, &buff, q->dec)) + return QValidator::Invalid; if (buff.isEmpty()) - return Intermediate; + return QValidator::Intermediate; - if (b >= 0 && buff.startsWith('-')) - return Invalid; + if (q->b >= 0 && buff.startsWith('-')) + return QValidator::Invalid; - if (t < 0 && buff.startsWith('+')) - return Invalid; + if (q->t < 0 && buff.startsWith('+')) + return QValidator::Invalid; bool ok, overflow; double i = QLocalePrivate::bytearrayToDouble(buff.constData(), &ok, &overflow); if (overflow) - return Invalid; + return QValidator::Invalid; if (!ok) - return Intermediate; + return QValidator::Intermediate; - if (i >= b && i <= t) - return Acceptable; + if (i >= q->b && i <= q->t) + return QValidator::Acceptable; - if (d->notation == StandardNotation) { - double max = qMax(qAbs(b), qAbs(t)); + 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) - return Invalid; + return QValidator::Invalid; } } - return Intermediate; + return QValidator::Intermediate; } diff --git a/tests/auto/qdoublevalidator/tst_qdoublevalidator.cpp b/tests/auto/qdoublevalidator/tst_qdoublevalidator.cpp index 26890b3..3470456 100644 --- a/tests/auto/qdoublevalidator/tst_qdoublevalidator.cpp +++ b/tests/auto/qdoublevalidator/tst_qdoublevalidator.cpp @@ -214,6 +214,10 @@ void tst_QDoubleValidator::validate_data() arabicNum += QChar(1643); arabicNum += QChar(1636); QTest::newRow("arabic") << "ar" << 0.0 << 20.0 << 2 << arabicNum << ACC << ACC; + + QTest::newRow("data_QTBUG_14935-1") << "de" << 0.0 << 1.0 << 5 << QString("0.31") << ACC << ACC; + QTest::newRow("data_QTBUG_14935-2") << "de" << 0.0 << 1000000.0 << 5 << QString("3.123") << ACC << ACC; + QTest::newRow("data_QTBUG_14935-3") << "de" << 0.0 << 1000000.0 << 5 << QString("123,345.678") << ACC << ACC; } void tst_QDoubleValidator::validate() -- cgit v0.12