The Link Your Class | http://t.csdnimg.cn/xdDRg |
---|---|
The Link of Requirement of This Assignment | http://t.csdnimg.cn/mt5IU |
The Aim of This Assignment | complete the calculator with a visual interface |
MU STU ID and FZU STU ID | 21124671_832101213 |
Link to the finished project code: code address
CONTENTS
I. PSP form
Personal Software Process Stages | Estimated Time(minutes) | Actual Time(minutes) |
---|---|---|
Planning | ||
• Estimate | 20 | 10 |
Development | ||
• Analysis | 20 | 20 |
• Design Spec | 30 | 30 |
• Design Review | 10 | 10 |
• Coding Standard | 30 | 30 |
• Design | 30 | 30 |
• Coding | 90 | 120 |
• Code Review | 30 | 30 |
• Test | 10 | 10 |
Reporting | ||
• Test Repor | 60 | 60 |
• Size Measurement | 10 | 10 |
• Postmortem & Process Improvement Plan | 10 | 10 |
Sum | 350 | 370 |
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
- Create QT project, open the .ui file and build the initial visual interface through QT. Confirm the position and funtion of each button.
- Adjust the window and button size, color step by step according to the calculator of Windows.
- After previewing and confirmation, start coding.
- The code as a whole is concatenated with conditional statements to distinguish the functions of different buttons.
- Refine the code after debugging.
- Pack and upload.
- Finished.
Flowchart
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.