From 590d01c36c4f0040cb8133f5bf4c059fd84721fc Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 4 Aug 2022 11:43:09 +0200 Subject: [PATCH] Don't skip QDoubleConverter's digit-count check for non-whole bound Using convertDoubleTo() to get a whole number, from which to determine the number of digits we're allowed before the fractional part, fails if the double isn't a whole number, which lead to the skip being checked. Use qFloor() of the double (as this should have as many digits as the double had before its decimal point, which is what we care about; qCeil() might round up to a power of ten). This amends commit ff6d2cb0d5779d81e89d94d65c8d164602fa2567 Fixes: QTBUG-105341 Change-Id: I4e0105d4602682c59e9830ec9a37556c96db884e Reviewed-by: Thiago Macieira (cherry picked from commit a79de46ac59e045fbbbb8e0490ea6981b041be73) Reviewed-by: Qt Cherry-pick Bot --- src/gui/util/qvalidator.cpp | 6 +++++- .../auto/gui/util/qdoublevalidator/tst_qdoublevalidator.cpp | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/gui/util/qvalidator.cpp b/src/gui/util/qvalidator.cpp index f71a66c98c3..8be08ebd2a2 100644 --- a/src/gui/util/qvalidator.cpp +++ b/src/gui/util/qvalidator.cpp @@ -658,7 +658,11 @@ QValidator::State QDoubleValidatorPrivate::validateWithLocale(QString &input, QL if (notation == QDoubleValidator::StandardNotation) { double max = qMax(qAbs(q->b), qAbs(q->t)); qlonglong v; - if (convertDoubleTo(max, &v)) { + // Need a whole number to pass to convertDoubleTo() or it fails. Use + // floor, as max is positive so this has the same number of digits + // before the decimal point, where qCeil() might take us up to a power + // of ten, adding a digit. + if (convertDoubleTo(qFloor(max), &v)) { qlonglong n = pow10(numDigits(v)); // 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 diff --git a/tests/auto/gui/util/qdoublevalidator/tst_qdoublevalidator.cpp b/tests/auto/gui/util/qdoublevalidator/tst_qdoublevalidator.cpp index 7818e83f09d..225ac14c7f5 100644 --- a/tests/auto/gui/util/qdoublevalidator/tst_qdoublevalidator.cpp +++ b/tests/auto/gui/util/qdoublevalidator/tst_qdoublevalidator.cpp @@ -143,6 +143,7 @@ void tst_QDoubleValidator::validate_data() QTest::newRow("data56") << "C" << 1229.0 << 1231.0 << -1 << QString("123E+") << ITM << INV; QTest::newRow("data57") << "C" << 1229.0 << 1231.0 << -1 << QString("123E+1") << ACC << INV; QTest::newRow("data58") << "C" << 0.0 << 100.0 << -1 << QString("0.0") << ACC << ACC; + QTest::newRow("overlong") << "C" << 0.0 << 99.9 << 2 << QString("1234.0") << ITM << INV; QTest::newRow("data_de0") << "de" << 0.0 << 100.0 << 1 << QString("50,0") << ACC << ACC; QTest::newRow("data_de1") << "de" << 00.0 << 100.0 << 1 << QString("500,0") << ITM << ITM;