PDA

View Full Version : Problem with qlineedit



mmegue
28th March 2014, 09:20
hie i am new to QT i want to create a BMI calculator if the result is shown in the QLineEdit the button BMIInterpretor must be enabled.so if i run the code below the app will crush.please help me.here is my code



#include "bmiCal.h"
bmiCal::bmiCal(){
setWindowTitle("BMI Calculator");
QGridLayout* layout = new QGridLayout(this);
QLabel* inputWeightRequest = new QLabel("Enter weight in kilograms ");
weightEntry = new QDoubleSpinBox();
QLabel* inputHeightRequest = new QLabel("Enter height in meters ");
heightEntry = new QDoubleSpinBox();
QPushButton* calculate = new QPushButton("Calculate");
QPushButton* BMIInterpretation = new QPushButton("BMIInterpretation");
BMIInterpretation->setEnabled(false);

QLabel* labelBmi = new QLabel("BMI ");
result = new QLineEdit();
QPushButton* clear = new QPushButton("Clear All");

layout->addWidget(inputWeightRequest, 0,0);
layout->addWidget(weightEntry, 0,1);
layout->addWidget(inputHeightRequest, 1,0);
layout->addWidget(heightEntry, 1,1);
layout->addWidget(kilograms, 0,3);
layout->addWidget(pounds, 0,4);
layout->addWidget(centimeters, 1,4);
layout->addWidget(meters, 1,3);
layout->addWidget(calculate, 2,1);
layout->addWidget(BMIInterpretation,3,3);
layout->addWidget(labelBmi,3,0);
layout->addWidget(result,3,1);
layout->addWidget(clear, 4,1);
this->setLayout(layout);
result->setReadOnly(true);

connect(calculate, SIGNAL(clicked()), this, SLOT(calculateBmi()));
connect (clear,SIGNAL(clicked()), this, SLOT(clear()));
connect (BMIInterpretation,SIGNAL(clicked()),this,SLOT(bmi interpretation()));

}
void bmiCal::calculateBmi() {
double wStr = weightEntry->value();
double hStr = heightEntry->value();
double bmi = wStr/(hStr*hStr);

if (result>=0){
result->setText(QString::number(bmi));
BMIInterpretation->setEnable(true);
}
else
QMessageBox::information(this,"IBM INTERPRETOR","THERE IS NOTHING TO DISPLAY");

}


void bmiCal::bmiinterpretation(){
QString res=result->text();
double res_nw=res.toDouble();

if (res_nw<18.5){
QMessageBox::information(this,"IBM INTERPRETATION", "Underweight");
}
if (res_nw>=18.5 && res_nw<=25){
QMessageBox::information(this,"IBM INTERPRETATION", "Healthy Weight");
}
if (res_nw>25 && res_nw<30){
QMessageBox::information(this,"IBM INTERPRETATION", "Overweight");
}
if (res_nw>=30){
QMessageBox::information(this,"IBM INTERPRETATION", "Obese");
}
}
void bmiCal::clear(){
weightEntry->clear();
heightEntry->clear();
result->clear();


}

anda_skoa
30th March 2014, 12:33
You constructor has a local variable called BMIInterpretation which most likely shadows a member that you want to use instead and which is current uninitialized.

Cheers,
_