Question

I have a QLineEdit which I use to get a double. But is there a more suitable way to get it? Here it is my code.

ui->lineEdit->setValidator(new QIntValidator(this));

QString XMAX=ui->lineEdit->text();
double xmax=XMAX.toDouble();
Was it helpful?

Solution

The canonical way to input a double is of course to use a QDoubleSpinBox.

If you insist on using a QLineEdit, you should use it together with a QDoubleValidator instead of your QIntValidator. I would just add a sanity check that something has been entered into the edit field:

double xmax;
if (ui->lineEdit->text()->isEmpty())
    xmax = numeric_limits<double>::quiet_NaN();
else
    xmax = ui->lineEdit->text().toDouble();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top