PDA

View Full Version : Problem displaying results of some equations properly in textBrowser



Pezdisp
8th February 2011, 00:34
This one should be easy for you guys.
It's just some simple calculations that I want to display in a textbrowser, but the numbers come out insanely large or not at all what they should be.
I get no errors or anything like that.
I tried different ways of making the numbers into strings, although the results where different they where still wrong.

I made the ui in Designer.

oppg2.h:


#ifndef OPPG2_H
#define OPPG2_H

#include "ui_oppg2.h"

class Oppg2 : public QWidget, private Ui::oppg2
{
Q_OBJECT

public:
Oppg2(QWidget *parent=0);

public slots:
void checkout();
void clear();
};

class PatientAccount : public QWidget, private Ui::oppg2
{

public:
void setDaysInHospital(int daysInHospital);
void setDaysInHospitalCost(int daysInHospitalCost);
void setTotalCharges(int totalCharges);
int getDaysInHospitalCost();
int getTotalCharges();

private:
const static int dailyRate=40;
int daysInHospital;
int daysInHospitalCost;
int totalCharges;
};

class Surgery : public QWidget, private Ui::oppg2
{

public:
void setNumSurgery(int numSurgery);
void setSurgeryCost(int surgeryCost);
void setTotalSurgeryCost(int totalSurgeryCost);
int getTotalSurgeryCost();

private:
int numSurgery;
int surgeryCost;
int totalSurgeryCost;
};

class Pharmacy : public QWidget, private Ui::oppg2
{

public:
void setNumMedication(int numMedication);
void setPharmacyCost(int pharmacyCost);
void setTotalPharmacyCost(int totalPharmacyCost);
int getTotalPharmacyCost();

private:
int numMedication;
int pharmacyCost;
int totalPharmacyCost;
};

#endif


oppg2.cpp:


#include <QtGui>
#include "oppg2.h"

//Start Oppg2 class

Oppg2::Oppg2(QWidget *parent)
{
setupUi(this);

connect(pushButton_checkout, SIGNAL(clicked()), this, SLOT(checkout()));
connect(pushButton_clear, SIGNAL(clicked()), this, SLOT(clear()));
}

void Oppg2::checkout()
{
PatientAccount PA;
Surgery S;
Pharmacy P;

int numDays;
int numSurgery;
int numMedication;

numDays=spinBox_days->value();
numSurgery=spinBox_surgery->value();
numMedication=spinBox_medication->value();

textBrowser->append("Number of days spent in hospital: " + QString::number(numDays));
textBrowser->append("Cost of hospitalstay: " + QString::number(PA.getDaysInHospitalCost()));
textBrowser->append("Type of surgery: " + comboBox_surgery->currentText());
textBrowser->append("Number of " + comboBox_surgery->currentText() + " surgery: " + QString::number(numSurgery));
textBrowser->append("Cost of surgery: " + QString::number(S.getTotalSurgeryCost()));
textBrowser->append("Type of medication: " + comboBox_medication->currentText());
textBrowser->append("Amount of " + comboBox_medication->currentText() + " medication: " + QString::number(numMedication));
textBrowser->append("Cost of medication: " + QString::number(P.getTotalPharmacyCost()));
textBrowser->append("Total cost of hospitalstay: " + QString::number(PA.getTotalCharges()));
}

void Oppg2::clear()
{
spinBox_days->setValue(0);
spinBox_surgery->setValue(0);
spinBox_medication->setValue(0);
textBrowser->clear();
}

//End Oppg2 class

//Start PatientAccount class

void PatientAccount::setDaysInHospital(int daysInHospital)
{
daysInHospital=spinBox_days->value();
}

void PatientAccount::setDaysInHospitalCost(int daysInHospitalCost)
{
daysInHospitalCost=daysInHospital*dailyRate;
}

void PatientAccount::setTotalCharges(int totalCharges)
{
Surgery S;
int gTSC=S.getTotalSurgeryCost();
Pharmacy P;
int gTPC=P.getTotalPharmacyCost();
totalCharges=(daysInHospitalCost+gTSC+gTPC);
}

int PatientAccount::getDaysInHospitalCost()
{
return daysInHospitalCost;
}

int PatientAccount::getTotalCharges()
{
return totalCharges;
}

//End PatientAccount class

//Start Surgery class

void Surgery::setNumSurgery(int numSurgery)
{
numSurgery=spinBox_surgery->value();
}

void Surgery::setSurgeryCost(int surgeryCost)
{
QString surgery;

surgery=comboBox_surgery->currentText();

if (surgery=="Brain") surgeryCost=220;
else if (surgery=="Heart") surgeryCost=200;
else if (surgery=="Eye") surgeryCost=90;
else if (surgery=="Arm") surgeryCost=110;
else if (surgery=="Leg") surgeryCost=120;
}

void Surgery::setTotalSurgeryCost(int totalSurgeryCost)
{
totalSurgeryCost=(numSurgery)*(surgeryCost);
}

int Surgery::getTotalSurgeryCost()
{
return totalSurgeryCost;
}

//End Surgery class

//Start Pharmacy class

void Pharmacy::setNumMedication(int numMedication)
{
numMedication=spinBox_medication->value();
}

void Pharmacy::setPharmacyCost(int pharmacyCost)
{

QString medication;

medication=comboBox_medication->currentText();

if (medication=="Fentanyl") pharmacyCost=30;
else if (medication=="Naproxen") pharmacyCost=15;
else if (medication=="Oxycontin") pharmacyCost=35;
else if (medication=="Valium") pharmacyCost=10;
else if (medication=="Vicodin") pharmacyCost=25;
}

void Pharmacy::setTotalPharmacyCost(int totalPharmacyCost)
{
totalPharmacyCost=(numMedication)*(pharmacyCost);
}

int Pharmacy::getTotalPharmacyCost()
{
return totalPharmacyCost;
}

//End Pharmacy class


All help appreciated, thanks.

tbscope
8th February 2011, 04:47
To me, this looks completely wrong.

Where is your ui pointer?
Why do you set values like this?


void Pharmacy::setTotalPharmacyCost(int totalPharmacyCost)
{
totalPharmacyCost=(numMedication)*(pharmacyCost);
}
This will NOT do what you think it does or want it to do.
Remove the parameter from the function.

JohannesMunk
8th February 2011, 14:15
You need to change the parameter name of the setter function to something else than the member variable you want to set.

Otherwise you just set the value of the local parameter and your member variable never gets set. What you observe are uninitialized values.

To recieve values from your ui, connect the appropriate change signals to your setter functions, which you need to declare as slots then.

HIH

Johannes