Uroc327 Uroc327 - 1 month ago 13
C++ Question

QDoubleValidator accepts multiple decimal points

I'm using a

QDoubleValidator
for my
QLineEdit
. The application locale (set in QtCreator) is
QLocale::German
.

Now when I enter a valid double (either using a dot or a comma as decimal separator) writing to the textedit as well as converting the string to a float works perfectly fine. But the validator also lets me write stuff with multiple decimal separators. Strings like
123.567,890
or
,,03.4...
get validated but can't get converted into a float.

Is there a way to tell
QDoubleValidator
to only validate real numbers and not just strings without alphabetical characters?

I basically want to have a validator, that only validates strings, that can get converted to floats
using either the default locale or the german locale.

Answer

I have not used the QDoubleValidator so far but I could achieve such behaviour by using a QRegExpValidator:

QRegExpValidator* rxv = new QRegExpValidator(QRegExp("[+-]?\\d*[\\.,]?\\d+"), this);
lineedit->setValidator(rxv);
Comments