Uroc327 Uroc327 - 9 months ago 116
C++ Question

QDoubleValidator accepts multiple decimal points

I'm using a

for my
. The application locale (set in QtCreator) is

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
get validated but can't get converted into a float.

Is there a way to tell
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 Source

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);