PDA

View Full Version : check if value is int or numeric



Swankee
30th October 2009, 19:09
Last night I has asked for a validator to for checking if a number was an int and I got a good hand. One of my co-workers told me they're going to need a double. The code will throw an error for white space and for special characters so I'm not sure what the approach is. Thanks for the clarificatioin




double GateOverlapFactor::on_okButton_clicked()
{
QString isNumber = m_ui->lineEdit->text();
QString trueNumber = "";
QString str = m_ui->lineEdit->text();
double returnDouble;

foreach(QChar c, str)
{
if(c.isNumber()) //check numeric input
{
trueNumber = m_ui->lineEdit->text();

close();
}
if(isspace(true)) // Check for whitespace
{
m_ui->messageLbl->setText(m_ui->lineEdit->text() + "Enter a Numeric Value");
}
else // not numeric?, throw error
{
m_ui->messageLbl->setText(m_ui->lineEdit->text() + ": Is Invalid. Enter a Numeric Value");

}
}

returnDouble = trueNumber.toDouble();

return returnDouble;
}

RSX
30th October 2009, 20:48
If you want to enter only double values in line edit then all you need is:

QDoubleValidator* validator = new QDoubleValidator(this);
m_ui->lineEdit->setValidator(validator);

Swankee
10th November 2009, 15:06
If you want to enter only double values in line edit then all you need is:

QDoubleValidator* validator = new QDoubleValidator(this);
m_ui->lineEdit->setValidator(validator);

thank you so much, was on vacation last week