Solved Display a Double in QlineEdit
-
Gents;
I have a problem displaying the result of a calculation that is a double type in a QlineEdit. The result should be less than 1, 0.75 for example, the result shows 0 though.
Cheers;
Raouf
-
Sure Ronin, please find it below:
void CFAdvisor::SLFcalculate (QString) { int Crush = ui->Input_CrushResis->text().toInt(); double AVF = ui->Input_PropAVF->text().toDouble(); int Closure = ui->Input_Closure->text().toInt(); int BHFP = ui->Input_BHFP->text().toInt(); int Net = ui->Input_NetPress->text().toInt(); double SLFresult = (Closure + Net - BHFP) / Crush; ui->label_SLFratio->setText(QString::number(SLFresult)); ui->Output_SLFresult->setText(QString::number(SLFresult)); }
-
How do you set QLineEdit with a double?
-
Hi,
what you say looks like you are rounding the double number:double x=0.75; QString xstr=QString::number(x); lineEdit->setText(xstr);
should do the trick.
-Michael. -
@koahnig
I want to display a double result in QLineEdit or in a QLabel, i tried both but it is just showing 0. The resut is less than 1. -
@Abderaouf Can you show us your code?
-
Sure Ronin, please find it below:
void CFAdvisor::SLFcalculate (QString) { int Crush = ui->Input_CrushResis->text().toInt(); double AVF = ui->Input_PropAVF->text().toDouble(); int Closure = ui->Input_Closure->text().toInt(); int BHFP = ui->Input_BHFP->text().toInt(); int Net = ui->Input_NetPress->text().toInt(); double SLFresult = (Closure + Net - BHFP) / Crush; ui->label_SLFratio->setText(QString::number(SLFresult)); ui->Output_SLFresult->setText(QString::number(SLFresult)); }
-
You devide an integer by an integer, so you will get an integer as result. Try:
(double) (Closure + Net - BHFP) / Crush
instead. -
I don't like C cast so:
double SLFresult = static_cast<double>(Closure + Net - BHFP) / static_cast<double>(Crush);
-
@VRonin ... as long as you have the first cast all will be good. The integer divisor will be promoted to double. This will make your code much more readable.
You can use QLocale to ensure your displays are properly international and localized.
For example:
ui->label_SLFratio->setText (QLocale ().toString (SLFResult));
ui->Output_SLFresult->setText (QLocale ().toString (SLFResult));
This formats the number for the default (currently set) locale. -
Hi,
Out of curiosity, why not use a QDoubleSpinBox to show that number ?
-