3

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();
SamuelNLP
  • 4,038
  • 9
  • 59
  • 102

1 Answers1

5

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();
Mehrwolf
  • 8,208
  • 2
  • 26
  • 38
  • 2
    Using QDoubleSpinBox I have to have the up and down arrow, which i don't want to. – SamuelNLP Sep 03 '12 at 11:34
  • 1
    @SamuelNLP: Ok, then stick with a line edit. Just use the correct validator and check that the line edit is not empty. – Mehrwolf Sep 03 '12 at 11:40
  • 3
    @SamuelNLP to get rid of the arrows you can simply use the method `setButtonSymbols(QAbstractSpinBox::NoButtons)` of the `QDoubleSpinBox` – Pankrates Aug 18 '14 at 01:17