Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-71914

QDoubleValidator::validate Accepts Numbers with Misaligned Separator Characters

    XMLWordPrintable

Details

    • Bug
    • Resolution: Cannot Reproduce
    • P2: Important
    • None
    • 5.11.2
    • Other
    • None
    • OS: Windows 7 Professional 64-bits
      Qt: Qt 5.11.2 MSVC 2015 64-bits
      IDE: Microsoft Visual Studio 2015
    • Windows

    Description

      Introduction
      Numbers can have group separator characters among digits for a better readability. For example, is better reading 1.000.000.000 than 1000000000 (since I am using Brazilian Portuguese, the group separator character is '.'). A number is said with misaligned group separator characters if it fails in separating groups of three digits (e.g., 1.0, 1.00, and 10.00).

      The problem
      The method QDoubleValidator::validate returns State::Acceptable when group separator characters are misaligned in a number; the correct output should be State::Intermediate. For example, if you input the string "10.00" to the method QDoubleValidator::validate, the result is State::Acceptable. However, if you try to convert the string "10.00" with the method QLocale::toDouble, it fails (see the code below).

      QString input = "10.00";
      int pos = -1;
      QDoubleValidator validator(-2000000000, 2000000000, 4);
      
      QValidator::State result = validator.validate(input, pos);
      qDebug() << "validate result = " << EnumToString(result);
      bool ok = false;
      double value = QLocale().toDouble(input, &ok);
      qDebug() << "QLocale convert result = " << ok;
      
      // Output:
      // validate result =  "Acceptable"
      // QLocale convert result =  false
      

      Interestingly enough, the equivalent method of the class QIntValidator produces the correct result (see the code below).

      QString input = "10.00";
      int pos = -1;
      QIntValidator validator(-2000000000, 2000000000);
      
      QValidator::State result = validator.validate(input, pos);
      qDebug() << "validate result = " << EnumToString(result);
      bool ok = false;
      int value = QLocale().toInt(input, &ok);
      qDebug() << "QLocale convert result = " << ok;
      
      // Output:
      // validate result =  "Intermediate"
      // QLocale convert result =  false
      

      Another problem with the QDoubleValidator is that the method QDoubleValidator::fixup does not fix anything up. Actually, by reading the source code, you can verify that the method fixup was not overridden in the QDoubleValidator class. Therefore, the default QValidator::fixup implementation, which is empty, is being executed. On the contrary, the QIntValidator::fixup method was properly overridden and produces the expected result (see the code below).

      QString input = "10.00";
      QDoubleValidator validator(-2000000000, 2000000000, 4);
      validator.fixup(input);
      qDebug() << "fixup result = " << input;
      
      // Output:
      // fixup result =  "10.00"
      
      QString input = "10.00";
      QIntValidator validator(-2000000000, 2000000000);
      validator.fixup(input);
      qDebug() << "fixup result = " << input;
      
      // Output:
      // fixup result =  "1.000"
      

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              Eddy Edward Welbourne
              sauloapessoa Saulo Pessoa
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes