Calculator for windows based on qt and C++

The Link Your Classhttp://t.csdnimg.cn/xdDRg
The Link of Requirement of This Assignmenthttp://t.csdnimg.cn/mt5IU
The Aim of This Assignmentcomplete the calculator with a visual interface
MU STU ID and FZU STU ID21124671_832101213

Link to the finished project code: code address

I. PSP form

Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning
• Estimate2010
Development
• Analysis2020
• Design Spec3030
• Design Review1010
• Coding Standard3030
• Design3030
• Coding90120
• Code Review3030
• Test1010
Reporting
• Test Repor6060
• Size Measurement1010
• Postmortem & Process Improvement Plan1010
Sum350370

II. Problem-solving ideas

In order to implement the calculator, I firstly searched for a variety of methods on Internet. After a simple search, I chose to use qt to complete the calculator. For convenience, I chose to use visual studio to assist.
QT is very easy and convenient to construct a visual interface. Then it is possible to write the logic of the individual buttons through visual studio.
I can have a basic understanding of QT through the project. I learnt how to construct the basic visual interface as well.

III. Design and implementation process

  1. Create QT project, open the .ui file and build the initial visual interface through QT. Confirm the position and funtion of each button.
  2. Adjust the window and button size, color step by step according to the calculator of Windows.
  3. After previewing and confirmation, start coding.
  4. The code as a whole is concatenated with conditional statements to distinguish the functions of different buttons.
  5. Refine the code after debugging.
  6. Pack and upload.
  7. Finished.

Flowchart

The flowchart of the key funtions for the calculator

IV. Code description

style sheet

*
{
 border:none;
 background-color:rgb(238, 236, 236);
}

QPushButton
{
 background-color:rgb(243, 243, 243);
}

QPushButton:hover
{
 border:1px solid rgb(193, 193, 193);
 background-color:rgb(220, 220, 220);
}

QPushButton#btn_0,#btn_1,#btn_2,#btn_3,#btn_4,#btn_5,#btn_6,#btn_7,#btn_8,#btn_9
{
 font:bold 12pt'微软雅黑';
 background-color:rgb(252, 252, 252);
}

QPushButton#btn_0:hover,#btn_1:hover,#btn_2:hover,#btn_3:hover,#btn_4:hover,#btn_5:hover,#btn_6:hover,#btn_7:hover,#btn_8:hover,#btn_9:hover
{
 border:1px solid rgb(193, 193, 193);
 background-color:rgb(220, 220, 220);
}

The color of the button is different in different areas and under different circumstances.
The size and layout is adjusted in the properties and layout.

header file

#pragma once

#include <QtWidgets/QWidget>
#include "ui_calculator.h"

QT_BEGIN_NAMESPACE
namespace Ui { class calculatorClass; };
QT_END_NAMESPACE

class calculator : public QWidget
{
    Q_OBJECT

public:
    calculator(QWidget *parent = nullptr);
    ~calculator();
    void iniUI();
    bool isInteger(double n);
public slots:
    void onButtonGroupClicked(QAbstractButton* btn);
private:
    Ui::calculatorClass *ui;
    QVector<QVariant> vec;
    QString prevBtn;
};

source file

#include "calculator.h"
#include <qbuttongroup.h>
#include <qdebug.h>
#include <qvector.h>
#include <iomanip>

calculator::calculator(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::calculatorClass())
{
    ui->setupUi(this);
    iniUI();
}

calculator::~calculator()
{
    delete ui;
}

void calculator::iniUI()
{
    auto buttonGroup = new QButtonGroup(this);//to save buttons
    auto btnlist = findChildren<QPushButton*>();
    for (auto btn : btnlist)
    {
        buttonGroup->addButton(btn);
    }//find all buttons
    
    connect(buttonGroup, QOverload<QAbstractButton *>::of(&QButtonGroup::buttonClicked), this, &calculator::onButtonGroupClicked);
    vec.resize(5);
}

void calculator::onButtonGroupClicked(QAbstractButton* btn)
{
    qreal val = ui->lineEdit->text().toDouble();
    qInfo() << btn->text(); 
    QString name = btn->text();
    

    if (name>="0" && name<="9" ||name ==".")
    {
        if (name != "."&& ui->lineEdit->text()=="0")  ui->lineEdit->clear();

        if (prevBtn == "+"|| prevBtn == "-" || prevBtn == "*" || prevBtn == "/")
        {
            ui->lineEdit->clear();
        }
        if (name == "." && !isInteger(val));
        else ui->lineEdit->insert(name);
    }//for digit
    else if (name == "+")
    {
        if (vec[2].isNull())
        {
            vec[0] = val;
            vec[1] = "+";
        }
    }
    else if (name == "-")
    {
        if (vec[2].isNull())
        {
            vec[0] = val;
            vec[1] = "-";
        }
    }
    else if (name == "*")
    {
        if (vec[2].isNull())
        {
            vec[0] = val;
            vec[1] = "*";
        }
    }
    else if (name == "/")
    {
        if (vec[2].isNull())
        {
            vec[0] = val;
            vec[1] = "/";
        }
    }
    else if (name == "=")
    {
        vec[2] = val;
        vec[3] = "=";
        if (vec[1] == "+")
        {
            vec[4] = vec[0].toDouble() + vec[2].toDouble();
        }
        if (vec[1] == "-")
        {
            vec[4] = vec[0].toDouble() - vec[2].toDouble();
        }
        if (vec[1] == "*")
        {
            vec[4] = vec[0].toDouble() * vec[2].toDouble();
        }
        if (vec[1] == "/")
        {
            vec[4] = vec[0].toDouble() / vec[2].toDouble();
        }
        floor(vec[4].toDouble() + 0.05);
        ui->lineEdit->setText(vec[4].toString());
        QVariant temp = vec[4];
        vec.clear();
        vec.resize(5);
        vec[1] = temp;
    }
    else if (name=="C")
    {
        ui->lineEdit->clear();
        ui->lineEdit->insert("0");
        ui->lineEdit_exp->clear();
        vec.clear();
        vec.resize(5);
    }//clear
    else if (name == "CE")
    {
        ui->lineEdit->clear();
        ui->lineEdit->insert("0");
    }//clear error
    else if (name == "Del")
    {
        if (vec[1].isNull())
        {
            ui->lineEdit->setCursorPosition(ui->lineEdit->cursorPosition() - 1);
            ui->lineEdit->del();
        }
        else vec[1] = NULL;
    }
    else if (name == "+/-")
    {
        QString temp = ui->lineEdit->text();
        if (val >= 0) ui->lineEdit->setText("-" + temp);
        else  ui->lineEdit->setText(temp.mid(1,temp.length()-1));
    }

    ui->lineEdit_exp->clear();
    for (auto var : vec)
    {
        ui->lineEdit_exp->insert(var.toString());
    }

    prevBtn = name;
}//functions of buttons  

bool calculator::isInteger(double n)
{
    if (n - int(n) == 0)
    {
        return true;
    }
    else return false;
}

The source code is concatenated with conditional statements to distinguish the functions of different buttons.

V. Result functions

在这里插入图片描述

VI. Summary

The calculator remains to be perfected. The calculator can work with only 2 numbers and complex calculations with multiple numbers are impossible. You can only do further operations after you have calculated a result, namely “=”.
Through the project, I have a preliminary understanding of the project process. I learn how to build a simple visual interface by qt, code with visual studio and connect both of them.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值