Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to QLineEdit to get a double

Tags:

qt

qlineedit

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();
like image 532
SamuelNLP Avatar asked Jan 28 '26 10:01

SamuelNLP


1 Answers

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();
like image 129
Mehrwolf Avatar answered Jan 31 '26 07:01

Mehrwolf