Results 1 to 2 of 2

Thread: Problem with qlineedit

  1. #1
    Join Date
    Aug 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with qlineedit

    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

    Qt Code:
    1. #include "bmiCal.h"
    2. bmiCal::bmiCal(){
    3. setWindowTitle("BMI Calculator");
    4. QGridLayout* layout = new QGridLayout(this);
    5. QLabel* inputWeightRequest = new QLabel("Enter weight in kilograms ");
    6. weightEntry = new QDoubleSpinBox();
    7. QLabel* inputHeightRequest = new QLabel("Enter height in meters ");
    8. heightEntry = new QDoubleSpinBox();
    9. QPushButton* calculate = new QPushButton("Calculate");
    10. QPushButton* BMIInterpretation = new QPushButton("BMIInterpretation");
    11. BMIInterpretation->setEnabled(false);
    12.  
    13. QLabel* labelBmi = new QLabel("BMI ");
    14. result = new QLineEdit();
    15. QPushButton* clear = new QPushButton("Clear All");
    16.  
    17. layout->addWidget(inputWeightRequest, 0,0);
    18. layout->addWidget(weightEntry, 0,1);
    19. layout->addWidget(inputHeightRequest, 1,0);
    20. layout->addWidget(heightEntry, 1,1);
    21. layout->addWidget(kilograms, 0,3);
    22. layout->addWidget(pounds, 0,4);
    23. layout->addWidget(centimeters, 1,4);
    24. layout->addWidget(meters, 1,3);
    25. layout->addWidget(calculate, 2,1);
    26. layout->addWidget(BMIInterpretation,3,3);
    27. layout->addWidget(labelBmi,3,0);
    28. layout->addWidget(result,3,1);
    29. layout->addWidget(clear, 4,1);
    30. this->setLayout(layout);
    31. result->setReadOnly(true);
    32.  
    33. connect(calculate, SIGNAL(clicked()), this, SLOT(calculateBmi()));
    34. connect (clear,SIGNAL(clicked()), this, SLOT(clear()));
    35. connect (BMIInterpretation,SIGNAL(clicked()),this,SLOT(bmiinterpretation()));
    36.  
    37. }
    38. void bmiCal::calculateBmi() {
    39. double wStr = weightEntry->value();
    40. double hStr = heightEntry->value();
    41. double bmi = wStr/(hStr*hStr);
    42.  
    43. if (result>=0){
    44. result->setText(QString::number(bmi));
    45. BMIInterpretation->setEnable(true);
    46. }
    47. else
    48. QMessageBox::information(this,"IBM INTERPRETOR","THERE IS NOTHING TO DISPLAY");
    49.  
    50. }
    51.  
    52.  
    53. void bmiCal::bmiinterpretation(){
    54. QString res=result->text();
    55. double res_nw=res.toDouble();
    56.  
    57. if (res_nw<18.5){
    58. QMessageBox::information(this,"IBM INTERPRETATION", "Underweight");
    59. }
    60. if (res_nw>=18.5 && res_nw<=25){
    61. QMessageBox::information(this,"IBM INTERPRETATION", "Healthy Weight");
    62. }
    63. if (res_nw>25 && res_nw<30){
    64. QMessageBox::information(this,"IBM INTERPRETATION", "Overweight");
    65. }
    66. if (res_nw>=30){
    67. QMessageBox::information(this,"IBM INTERPRETATION", "Obese");
    68. }
    69. }
    70. void bmiCal::clear(){
    71. weightEntry->clear();
    72. heightEntry->clear();
    73. result->clear();
    74.  
    75.  
    76. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 30th March 2014 at 12:30. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with qlineedit

    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,
    _

Similar Threads

  1. QLineEdit validator() problem
    By ayanda83 in forum Newbie
    Replies: 2
    Last Post: 30th August 2012, 23:40
  2. QlineEdit + Sql Problem
    By anouar2002 in forum Newbie
    Replies: 15
    Last Post: 19th January 2012, 14:29
  3. some problem with QLineEdit keyPressEvent
    By yxtx1984 in forum Qt Programming
    Replies: 0
    Last Post: 5th November 2010, 09:03
  4. QLineEdit problem
    By kumarpraveen in forum Newbie
    Replies: 5
    Last Post: 12th July 2010, 12:19
  5. QLineEdit Problem
    By newermind in forum Qt Programming
    Replies: 2
    Last Post: 18th August 2009, 14:14

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.