How to validate the price on Qt?

0

I would like to know how to validate the price on Qt, as follows:

Examples accepted: 10,00 / 100,00 / 1.000,00 = true

My code, (but is validating true: 10 / 100 )

 bool ok;
 QLocale::setDefault(QLocale(QLocale::Portuguese, QLocale::Brazil));
 QLocale brazil; // Constructs a default QLocale
 brazil.toDouble(ui->price->text(), &ok);
 qDebug() <<  ok;
    
asked by anonymous 17.03.2014 / 14:02

1 answer

3

If you want to precisely validate a number in the form of a price (exactly two decimal places, not in the exponential format, comma separator, point thousands separator), you should use something more precise than trying a conversion to double. Do with a regex:

QRegularExpression priceRe("^[0-9]{1,3}(\.[0-9]{3})*,[0-9]{2}$");
QString price = "1.234,53";
if (priceRe.match(price).hasMatch())
    qDebug() << "matched";

regexplained

    
17.03.2014 / 14:43