GUI Based Calculator using C++ Qt Framework














































GUI Based Calculator using C++ Qt Framework



                                         Widget Based Application
How to build widget application:

  • for creating a widget based application, basic requirements: Qt,C++.
  • In Qt, Click on New Project .
  • A dialog box appears, Select Qt Widget Application.
  • Select the Kit.
Program:

Design


Drop QLineEdit in window -> Name it Display -> Preferred & Expanding
Right Click Window -> Layout -> Layout in Grid
Create QPushButtons 5 wide and 4 deep -> Preferred & Preferred
7 8 9 / M+
4 5 6 * M-
1 2 3 + M
AC 0 +/- - =
Name them Button0 - Button9, Add, Subtract, Multiply, Divide, Equals, ChangeSign, Clear, MemAdd, MemClear, MemGet
Line Edit Stylesheet
QLineEdit {
     background-color: gray; border: 1px solid gray;
color: #ffffff;}
10. PushButton Stylesheet
QPushButton {
     background-color: #C0C0C0; border: 1px solid gray;
border-radius: 0px;
padding: 5px;
}
QPushButton:pressed {
    background-color: #A9A9A9; border: 1px solid gray;
border-radius: 0px;
padding: 5px;
}
 

 
#ifndef CALC_H
#define CALC_H
 
#include <QMainWindow>
 
namespace Ui {
class Calc;
}
 
class Calc : public QMainWindow
{
    Q_OBJECT
 
public:
    
    explicit Calc(QWidget *parent = 0);
    ~Calc();
 
private:
    Ui::Calc *ui;
 
private slots :
    void NumPressed();
    void MathButtonPressed();
    void EqualButtonPressed();
    void ChangeNumberSign();
};
 
#endif // CALC_H
 

 
#include "calc.h"
#include "ui_calc.h"
 
double calcVal = 0.0;
 
bool divTrigger = false;
bool multTrigger = false;
bool addTrigger = false;
bool subTrigger = false;
 

Calc::Calc(QWidget *parent) :
 
  
    QMainWindow(parent),
 
  
    ui(new Ui::Calc)
{
 
       ui->setupUi(this);
 
       ui->Display->setText(QString::number(calcVal));
 
       QPushButton *numButtons[10];
 
  
    for(int i = 0; i < 10; ++i){
        QString butName = "Button" + QString::number(i);
 
  
        numButtons[i] = Calc::findChild<QPushButton *>(butName);
 
  
        connect(numButtons[i], SIGNAL(released()), this,
                SLOT(NumPressed()));
    }
 
  
    connect(ui->Add, SIGNAL(released()), this,
            SLOT(MathButtonPressed()));
    connect(ui->Subtract, SIGNAL(released()), this,
            SLOT(MathButtonPressed()));
    connect(ui->Multiply, SIGNAL(released()), this,
            SLOT(MathButtonPressed()));
    connect(ui->Divide, SIGNAL(released()), this,
            SLOT(MathButtonPressed()));
 
  
    connect(ui->Equals, SIGNAL(released()), this,
            SLOT(EqualButtonPressed()));
 
 
    connect(ui->ChangeSign, SIGNAL(released()), this,
            SLOT(ChangeNumberSign()));
 
 
}
 
Calc::~Calc()
{
    delete ui;
}
 
void Calc::NumPressed(){
 
     QPushButton *button = (QPushButton *)sender();
 
     QString butVal = button->text();
 
     QString displayVal = ui->Display->text();
 
    if((displayVal.toDouble() == 0) || (displayVal.toDouble() == 0.0)){
 
        ui->Display->setText(butVal);
 
    } else {
        QString newVal = displayVal + butVal;
 
   
        double dblNewVal = newVal.toDouble();
 
        ui->Display->setText(QString::number(dblNewVal, 'g', 16));
 
    }
}
 
void Calc::MathButtonPressed(){
 
    divTrigger = false;
    multTrigger = false;
    addTrigger = false;
    subTrigger = false;
 
    QString displayVal = ui->Display->text();
    calcVal = displayVal.toDouble();
 
    QPushButton *button = (QPushButton *)sender();
 
    QString butVal = button->text();
 
    if(QString::compare(butVal, "/", Qt::CaseInsensitive) == 0){
        divTrigger = true;
    } else if(QString::compare(butVal, "*", Qt::CaseInsensitive) == 0){
        multTrigger = true;
    } else if(QString::compare(butVal, "+", Qt::CaseInsensitive) == 0){
        addTrigger = true;
    } else {
        subTrigger = true;
    }
 
    ui->Display->setText("");
 
}
 
void Calc::EqualButtonPressed(){
 
    double solution = 0.0;
 
 
    QString displayVal = ui->Display->text();
    double dblDisplayVal = displayVal.toDouble();
 
 
    if(addTrigger || subTrigger || multTrigger || divTrigger ){
        if(addTrigger){
            solution = calcVal + dblDisplayVal;
        } else if(subTrigger){
            solution = calcVal - dblDisplayVal;
        } else if(multTrigger){
            solution = calcVal * dblDisplayVal;
        } else {
            solution = calcVal / dblDisplayVal;
        }
    }
 
      ui->Display->setText(QString::number(solution));
 
}
 
void Calc::ChangeNumberSign(){
 
 
    QString displayVal = ui->Display->text();
 
    QRegExp reg("[-+]?[0-9.]*");
 
    if(reg.exactMatch(displayVal)){
        double dblDisplayVal = displayVal.toDouble();
        double dblDisplayValSign = -1 * dblDisplayVal;
 
     
        ui->Display->setText(QString::number(dblDisplayValSign));
    }
 
}
 
 
 
 

 
#include "calc.h"
#include <QApplication>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Calc w;
    w.show();
 
    return a.exec();
}
 


Output:

Image result for calculator using qt

Reference Youtube Videos:


Comments