SamuelNLP
SamuelNLP

Reputation: 4136

Alternative to QLineEdit to get a double

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

Upvotes: 3

Views: 8492

Answers (1)

Mehrwolf
Mehrwolf

Reputation: 8527

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

Upvotes: 5

Related Questions