Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Creating BMI index widget using Qt

From Wiki
Jump to: navigation, search
Article Metadata
Code ExampleCompatibility
Platform(s):
Symbian
Article
Keywords: QMainWindow, QWidget
Created: james1980 (24 Mar 2010)
Last edited: hamishwillee (11 Oct 2012)

This article demonstrates how to create a BMI index widget using Qt. 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.

Contents

Header File

  #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

#include "QMessageBox"
#include "bmi.h"
#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

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.

Bmi1.jpg

Bmi2.jpg

Reference

BMI widget using web runtime

Source Code

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

This page was last modified on 11 October 2012, at 01:16.
90 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×