Creating BMI index widget using Qt

From Qt Wiki
Jump to navigation Jump to search

Template:ArticleMetaData Template:Abstract BMI Index can be use to check your health and if you are a over weight or under weight. This article is intended to teach a beginner to code a basic application in Qt. Concepts like Signal, slots and data handling are used in this application.

Header File[edit | edit source]

 #ifndef BMI_H
 #define BMI_H
 #include <QMainWindow>
 namespace Ui {
   class BMI;
 }
 class BMI : public QMainWindow {
   Q_OBJECT
 public:
   BMI(QWidget *parent = 0);
   ~BMI();
 protected:
   void changeEvent(QEvent *e);
 private:
   Ui::BMI *ui;
 private slots:
   void on_lineEdit_2_returnPressed();
   void on_lineEdit_returnPressed();
   void on_actionCalculate_triggered();
   void on_actionExit_triggered();
 };
 #endif // BMI_H

Source file[edit | edit source]

  1. include "QMessageBox"
  2. include "bmi.h"
  3. include "ui_bmi.h"
 BMI::BMI(QWidget *parent) :
   QMainWindow(parent),
   ui(new Ui::BMI)
 {
   ui->setupUi(this);
   QValidator *validator = new QIntValidator(1, 1000, this);
    // the edit lineedit will only accept integers between 1 and 1000
   ui->lineEdit->setValidator(validator);
   ui->lineEdit_2->setValidator(validator);
 }
  BMI::~BMI()
 {
   delete ui;
 }
 void BMI::changeEvent(QEvent *e)
 {
   QMainWindow::changeEvent(e);
   switch (e->type()) {
   case QEvent::LanguageChange:
       ui->retranslateUi(this);
       break;
   default:
       break;
   }
 }
 void BMI::on_actionExit_triggered()
 {
   BMI::close();
 }
 void BMI::on_actionCalculate_triggered()
 {
    if(ui->lineEdit->text().compare("")!=0 && ui->lineEdit_2->text().compare("")!=0)
    {
       QString height = ui->lineEdit->text();
       QString weight = ui->lineEdit_2->text();
       int h = height.toInt();
       int w = weight.toInt();
       int index;
       index = (10000 * w) / (h * h);
       ui->lcdNumber->display(index);
    }
    else
    {
       QMessageBox msgBox;
       msgBox.setText("Please enter height & weight");
       msgBox.exec();
    }
 }
 void BMI::on_lineEdit_returnPressed()
 {
   BMI::on_actionCalculate_triggered();
 }
 void BMI::on_lineEdit_2_returnPressed()
 {
   BMI::on_actionCalculate_triggered();
 }

Output[edit | edit source]

If your BMindex is less than 18.5 you are considered to be underweight. If it is a greater than 25 you are over weight. And in between this two you are having a normal weight.

File:Bmi1.jpg

File:Bmi2.jpg

Reference[edit | edit source]

BMI widget using web runtime

Source Code[edit | edit source]

The full source code can be downloaded from here : Media:BMICalculator.zip