0
I would like to know how to validate the price in Qt, as follows:
Accepted examples: 10,00
/ 100,00
/ 1.000,00 = true
My code, (but it’s 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;
accepted example:
1.00,00
?– Math
@Math: heh, I missed. I missed a zero. Ae Math knows how to validate? I’ll be very grateful for the help. :)
– Hy-brazil
I’d love to help, but I really don’t know what it is
qt
... soh one more thing that left me in doubt on your question:mas ta validando true: 10 / 100 / 1000.00
means that the q has comma it ta returning false and only the examples without comma return true?– Math
Look, I just tested here with
brazil.toDouble("1000.00", &ok);
, for example, and it worked (I mean, returnedfalse
as expected).– Luiz Vieira
However, for values like
10
and100
(no point in the place of the comma) I believe that the validation will always be correct, because they are valid numbers in the locale of Brazil.– Luiz Vieira
You should explain your question better, I tried to help though, still got confused.
– Paulo Roberto Rosa
ah, now I understand, to return
true
has to have the.
separating the decimals and the,
if the entire part passes 999, this Voce expects @user628298 ?– Math
@Math: yes. True=1,000.00 false=1000.00
– Hy-brazil
it seems that Qlocale does not have this format check functionality you want, better search for other means of checking, for example regex.
– pepper_chico
@user628298 But, your code ALREADY does that. At least for the examples you just commented on.
– Luiz Vieira
@Luiz Vieira: Yes back false. I added wrong :)
– Hy-brazil
Ok, so your question really isn’t clear. : ) What’s the problem? Wouldn’t you NOT want them to validate values without the comma? If that’s the case, the @pepper_chico suggestion looks ideal: use a regular expression.
– Luiz Vieira
@Luizvieira to me seems that he does not want it to pass if it has no comma. This seems clear, and this Qlocale does not offer.
– pepper_chico
@pepper_chico It seems that way. But the question does not say that clearly.
– Luiz Vieira
@Luiz Vieira: better a regex or add one
código
if(ui->preco->text().length() <= 2){ qDebug() << text.sprintf("%6.2f", ui->preco->text().toDouble(); } ?código
– Hy-brazil
@Guilhermebernal just posted an answer that should help you. :)
– Luiz Vieira
Thank you, all of you :)
– Hy-brazil