Namespaces
Variants
Actions

How to make a simple calculator in Qt

Jump to: navigation, search
Article Metadata

Tested with
Devices(s): S60 Emulator

Compatibility
Platform(s): Qt

Article
Keywords: QLineEdit,QPushButton
Created: mind_freak (28 Mar 2009)
Last edited: hamishwillee (11 Oct 2012)

Contents

Introduction

This example demonstrates a simple QWidget based calculator.

The drag and drop pattern is used to built this calculator. Several PushButton are dragged and dropped to the form and the code of click event to button is written. Specific event is triggered on pressing button.

Preconditions

  • Download and install the Qt SDK.

UI design

Uiiiiiiiiii.JPG

Source code

Header File

#ifndef CALCULATOR_H
#define CALCULATOR_H
 
#include <QtGui/QWidget>
#include<QString>
 
namespace Ui
{
class calculatorClass;
}
 
class calculator : public QWidget
{
Q_OBJECT
 
public:
calculator(QWidget *parent = 0);
~calculator();
 
private:
Ui::calculatorClass *ui;
private:
QString str;
QString str1;
QString ch;
QString strResult;
QString text1;
 
public:
float num;
float num1;
float ans;
 
 
private slots:
void addi();
void subs();
void mult();
void divi();
void em();
void mone();
void mtwo();
void mthree();
void mfour();
void mfive();
void msix();
void mseven();
void meight();
void mnine();
void mzero();
void mreset();
void mon();
void moff();
void mdot();
};
 
#endif // CALCULATOR_H

Source code

#include "calculator.h"
#include "ui_calculator.h"
 
calculator::calculator(QWidget *parent)
: QWidget(parent), ui(new Ui::calculatorClass)
{
ui->setupUi(this);
text1="";
ui->line->setAlignment(Qt::AlignRight);
ui->line->setMaxLength(12);
ans=0;
 
QObject::connect(ui->but1,SIGNAL(clicked()),this,SLOT(addi()));
QObject::connect(ui->but2,SIGNAL(clicked()),this,SLOT(subs()));
QObject::connect(ui->but3,SIGNAL(clicked()),this,SLOT(mult()));
QObject::connect(ui->but4,SIGNAL(clicked()),this,SLOT(divi()));
QObject::connect(ui->equal,SIGNAL(clicked()),this,SLOT(em()));
QObject::connect(ui->one,SIGNAL(clicked()),this,SLOT(mone()));
QObject::connect(ui->two,SIGNAL(clicked()),this,SLOT(mtwo()));
QObject::connect(ui->three,SIGNAL(clicked()),this,SLOT(mthree()));
QObject::connect(ui->four,SIGNAL(clicked()),this,SLOT(mfour()));
QObject::connect(ui->five,SIGNAL(clicked()),this,SLOT(mfive()));
QObject::connect(ui->six,SIGNAL(clicked()),this,SLOT(msix()));
QObject::connect(ui->seven,SIGNAL(clicked()),this,SLOT(mseven()));
QObject::connect(ui->eight,SIGNAL(clicked()),this,SLOT(meight()));
QObject::connect(ui->nine,SIGNAL(clicked()),this,SLOT(mnine()));
QObject::connect(ui->zero,SIGNAL(clicked()),this,SLOT(mzero()));
QObject::connect(ui->reset,SIGNAL(clicked()),this,SLOT(mreset()));
QObject::connect(ui->on,SIGNAL(clicked()),this,SLOT(mon()));
QObject::connect(ui->off,SIGNAL(clicked()),this,SLOT(moff()));
QObject::connect(ui->dot,SIGNAL(clicked()),this,SLOT(mdot()));
}
 
calculator::~calculator()
{
delete ui;
}
void calculator::mdot()
{
text1+=ui->dot->text();
ui->line->setText(text1);
}
void calculator::moff()
{
ui->one->setEnabled(0);
ui->two->setEnabled(0);
ui->three->setEnabled(0);
ui->four->setEnabled(0);
ui->five->setEnabled(0);
ui->six->setEnabled(0);
ui->seven->setEnabled(0);
ui->eight->setEnabled(0);
ui->nine->setEnabled(0);
ui->zero->setEnabled(0);
ui->equal->setEnabled(0);
ui->but1->setEnabled(0);
ui->but2->setEnabled(0);
ui->but3->setEnabled(0);
ui->but4->setEnabled(0);
ui->off->setEnabled(0);
ui->reset->setEnabled(0);
ui->on->setEnabled(1);
ui->line->setEnabled(0);
ui->dot->setEnabled(0);
//ui->backspc->setEnabled(0);
ui->line->clear();
}
void calculator::mon()
{
ui->one->setEnabled(1);
ui->two->setEnabled(1);
ui->three->setEnabled(1);
ui->four->setEnabled(1);
ui->five->setEnabled(1);
ui->six->setEnabled(1);
ui->seven->setEnabled(1);
ui->eight->setEnabled(1);
ui->nine->setEnabled(1);
ui->zero->setEnabled(1);
ui->equal->setEnabled(1);
ui->but1->setEnabled(1);
ui->but2->setEnabled(1);
ui->but3->setEnabled(1);
ui->but4->setEnabled(1);
ui->off->setEnabled(1);
ui->reset->setEnabled(1);
ui->on->setEnabled(0);
ui->line->setEnabled(1);
ui->dot->setEnabled(1);
//ui->backspc->setEnabled(1);
 
}
void calculator::mreset()
{
text1="";
ui->line->clear();
}
void calculator::mone()
{
 
 
text1+=ui->one->text();
ui->line->setText(text1);
 
}
void calculator::mtwo()
{
 
text1+=ui->two->text();
ui->line->setText(text1);
 
}
void calculator::mthree()
{
 
text1+=ui->three->text();
ui->line->setText(text1);
 
}
void calculator::mfour()
{
 
text1+=ui->four->text();
ui->line->setText(text1);
 
}
void calculator::mfive()
{
 
text1+=ui->five->text();
ui->line->setText(text1);
 
}
void calculator::msix()
{
 
text1+=ui->six->text();
ui->line->setText(text1);
 
}
void calculator::mseven()
{
 
text1+=ui->seven->text();
ui->line->setText(text1);
 
}
void calculator::meight()
{
 
text1+=ui->eight->text();
ui->line->setText(text1);
 
}
void calculator::mnine()
{
 
text1+=ui->nine->text();
ui->line->setText(text1);
 
}
void calculator::mzero()
{
 
text1+=ui->zero->text();
ui->line->setText(text1);
 
}
void calculator::addi() //Addition
{
str=ui->line->text();
num=str.toFloat();
ch=ui->but1->text();
ui->line->clear();
text1="";
}
void calculator::subs() //Substraction
{
str=ui->line->text();
num=str.toFloat();
ch=ui->but2->text();
ui->line->clear();
text1="";
}
void calculator::mult() //Multiplaction
{
str=ui->line->text();
num=str.toFloat();
ch=ui->but3->text();
ui->line->clear();
text1="";
}
void calculator::divi() //Division
{
str=ui->line->text();
num=str.toFloat();
ch=ui->but4->text();
ui->line->clear();
text1="";
}
void calculator::em()
{
 
 
str1 = ui->line->text();
num1 = str1.toFloat();
if (ch=="+")
{
ans=num+num1;
}
else if(ch=="-")
{
ans=num-num1;
}
else if(ch=="*")
{
ans=num*num1;
}
else if(ch=="/")
{
ans=num/num1;
}
else
{
strResult = strResult.number(ans);
ui->line->setText(strResult);
}
strResult = strResult.number(ans);
ui->line->setText(strResult);
ch="";
text1=strResult;
}

Screenshots

Calculator001.jpg

This page was last modified on 11 October 2012, at 04:17.
651 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved