Results 1 to 3 of 3

Thread: Problem displaying results of some equations properly in textBrowser

  1. #1
    Join Date
    Feb 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem displaying results of some equations properly in textBrowser

    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:
    Qt Code:
    1. #ifndef OPPG2_H
    2. #define OPPG2_H
    3.  
    4. #include "ui_oppg2.h"
    5.  
    6. class Oppg2 : public QWidget, private Ui::oppg2
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. Oppg2(QWidget *parent=0);
    12.  
    13. public slots:
    14. void checkout();
    15. void clear();
    16. };
    17.  
    18. class PatientAccount : public QWidget, private Ui::oppg2
    19. {
    20.  
    21. public:
    22. void setDaysInHospital(int daysInHospital);
    23. void setDaysInHospitalCost(int daysInHospitalCost);
    24. void setTotalCharges(int totalCharges);
    25. int getDaysInHospitalCost();
    26. int getTotalCharges();
    27.  
    28. private:
    29. const static int dailyRate=40;
    30. int daysInHospital;
    31. int daysInHospitalCost;
    32. int totalCharges;
    33. };
    34.  
    35. class Surgery : public QWidget, private Ui::oppg2
    36. {
    37.  
    38. public:
    39. void setNumSurgery(int numSurgery);
    40. void setSurgeryCost(int surgeryCost);
    41. void setTotalSurgeryCost(int totalSurgeryCost);
    42. int getTotalSurgeryCost();
    43.  
    44. private:
    45. int numSurgery;
    46. int surgeryCost;
    47. int totalSurgeryCost;
    48. };
    49.  
    50. class Pharmacy : public QWidget, private Ui::oppg2
    51. {
    52.  
    53. public:
    54. void setNumMedication(int numMedication);
    55. void setPharmacyCost(int pharmacyCost);
    56. void setTotalPharmacyCost(int totalPharmacyCost);
    57. int getTotalPharmacyCost();
    58.  
    59. private:
    60. int numMedication;
    61. int pharmacyCost;
    62. int totalPharmacyCost;
    63. };
    64.  
    65. #endif
    To copy to clipboard, switch view to plain text mode 

    oppg2.cpp:
    Qt Code:
    1. #include <QtGui>
    2. #include "oppg2.h"
    3.  
    4. //Start Oppg2 class
    5.  
    6. Oppg2::Oppg2(QWidget *parent)
    7. {
    8. setupUi(this);
    9.  
    10. connect(pushButton_checkout, SIGNAL(clicked()), this, SLOT(checkout()));
    11. connect(pushButton_clear, SIGNAL(clicked()), this, SLOT(clear()));
    12. }
    13.  
    14. void Oppg2::checkout()
    15. {
    16. PatientAccount PA;
    17. Surgery S;
    18. Pharmacy P;
    19.  
    20. int numDays;
    21. int numSurgery;
    22. int numMedication;
    23.  
    24. numDays=spinBox_days->value();
    25. numSurgery=spinBox_surgery->value();
    26. numMedication=spinBox_medication->value();
    27.  
    28. textBrowser->append("Number of days spent in hospital: " + QString::number(numDays));
    29. textBrowser->append("Cost of hospitalstay: " + QString::number(PA.getDaysInHospitalCost()));
    30. textBrowser->append("Type of surgery: " + comboBox_surgery->currentText());
    31. textBrowser->append("Number of " + comboBox_surgery->currentText() + " surgery: " + QString::number(numSurgery));
    32. textBrowser->append("Cost of surgery: " + QString::number(S.getTotalSurgeryCost()));
    33. textBrowser->append("Type of medication: " + comboBox_medication->currentText());
    34. textBrowser->append("Amount of " + comboBox_medication->currentText() + " medication: " + QString::number(numMedication));
    35. textBrowser->append("Cost of medication: " + QString::number(P.getTotalPharmacyCost()));
    36. textBrowser->append("Total cost of hospitalstay: " + QString::number(PA.getTotalCharges()));
    37. }
    38.  
    39. void Oppg2::clear()
    40. {
    41. spinBox_days->setValue(0);
    42. spinBox_surgery->setValue(0);
    43. spinBox_medication->setValue(0);
    44. textBrowser->clear();
    45. }
    46.  
    47. //End Oppg2 class
    48.  
    49. //Start PatientAccount class
    50.  
    51. void PatientAccount::setDaysInHospital(int daysInHospital)
    52. {
    53. daysInHospital=spinBox_days->value();
    54. }
    55.  
    56. void PatientAccount::setDaysInHospitalCost(int daysInHospitalCost)
    57. {
    58. daysInHospitalCost=daysInHospital*dailyRate;
    59. }
    60.  
    61. void PatientAccount::setTotalCharges(int totalCharges)
    62. {
    63. Surgery S;
    64. int gTSC=S.getTotalSurgeryCost();
    65. Pharmacy P;
    66. int gTPC=P.getTotalPharmacyCost();
    67. totalCharges=(daysInHospitalCost+gTSC+gTPC);
    68. }
    69.  
    70. int PatientAccount::getDaysInHospitalCost()
    71. {
    72. return daysInHospitalCost;
    73. }
    74.  
    75. int PatientAccount::getTotalCharges()
    76. {
    77. return totalCharges;
    78. }
    79.  
    80. //End PatientAccount class
    81.  
    82. //Start Surgery class
    83.  
    84. void Surgery::setNumSurgery(int numSurgery)
    85. {
    86. numSurgery=spinBox_surgery->value();
    87. }
    88.  
    89. void Surgery::setSurgeryCost(int surgeryCost)
    90. {
    91. QString surgery;
    92.  
    93. surgery=comboBox_surgery->currentText();
    94.  
    95. if (surgery=="Brain") surgeryCost=220;
    96. else if (surgery=="Heart") surgeryCost=200;
    97. else if (surgery=="Eye") surgeryCost=90;
    98. else if (surgery=="Arm") surgeryCost=110;
    99. else if (surgery=="Leg") surgeryCost=120;
    100. }
    101.  
    102. void Surgery::setTotalSurgeryCost(int totalSurgeryCost)
    103. {
    104. totalSurgeryCost=(numSurgery)*(surgeryCost);
    105. }
    106.  
    107. int Surgery::getTotalSurgeryCost()
    108. {
    109. return totalSurgeryCost;
    110. }
    111.  
    112. //End Surgery class
    113.  
    114. //Start Pharmacy class
    115.  
    116. void Pharmacy::setNumMedication(int numMedication)
    117. {
    118. numMedication=spinBox_medication->value();
    119. }
    120.  
    121. void Pharmacy::setPharmacyCost(int pharmacyCost)
    122. {
    123.  
    124. QString medication;
    125.  
    126. medication=comboBox_medication->currentText();
    127.  
    128. if (medication=="Fentanyl") pharmacyCost=30;
    129. else if (medication=="Naproxen") pharmacyCost=15;
    130. else if (medication=="Oxycontin") pharmacyCost=35;
    131. else if (medication=="Valium") pharmacyCost=10;
    132. else if (medication=="Vicodin") pharmacyCost=25;
    133. }
    134.  
    135. void Pharmacy::setTotalPharmacyCost(int totalPharmacyCost)
    136. {
    137. totalPharmacyCost=(numMedication)*(pharmacyCost);
    138. }
    139.  
    140. int Pharmacy::getTotalPharmacyCost()
    141. {
    142. return totalPharmacyCost;
    143. }
    144.  
    145. //End Pharmacy class
    To copy to clipboard, switch view to plain text mode 

    All help appreciated, thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Problem displaying results of some equations properly in textBrowser

    To me, this looks completely wrong.

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

    Qt Code:
    1. void Pharmacy::setTotalPharmacyCost(int totalPharmacyCost)
    2. {
    3. totalPharmacyCost=(numMedication)*(pharmacyCost);
    4. }
    To copy to clipboard, switch view to plain text mode 
    This will NOT do what you think it does or want it to do.
    Remove the parameter from the function.

  3. #3
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem displaying results of some equations properly in textBrowser

    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

Similar Threads

  1. Time in textBrowser
    By NewLegend in forum Qt Programming
    Replies: 13
    Last Post: 12th October 2010, 05:37
  2. space in textBrowser
    By NewLegend in forum Qt Programming
    Replies: 4
    Last Post: 3rd September 2010, 20:04
  3. Replies: 0
    Last Post: 13th August 2010, 13:17
  4. How to solve simultanious equations...??
    By joseph in forum General Programming
    Replies: 2
    Last Post: 28th May 2008, 11:53
  5. Textbrowser issue
    By deekayt in forum Qt Programming
    Replies: 1
    Last Post: 29th October 2006, 08:51

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.