目录
一、题目描述:
二、创建工程:
1. 编辑
2.
3.
4. 默认
5.
6.
7.
8. 默认
9. 创建完成
三、UI界面设计:
1. 添加按钮
1.
2. 按钮界面
3. 按钮绑定快捷键
2. 文本框添加
1. 文本框字体
2. 默认文本
3. 文本对齐方式
3. 添加图片
1. 找到图片并添加到工程目录下
2.
3.
4. 图片名称
5.
6.添加-->添加前缀
7. 再次添加 -- > 添加文件,此次有 添加文件 了
8.选择图片文件
9. 成功添加文件 -->>> 运行一下
10. 成功
4. 按钮添加图片
1. -->> 选择资源
2. 选择对应的图片资源
3. 添加成功
5. UI界面大致样式
四、程序编写:
1. 思路:
2. 创建类:
1.
2.
3.
4.
5. 创建成功
6. 同上方式载创建一个 Calculate 类
3. data类:
(1)data.h
(2)data.cpp
1. 初始化数据
2. 数据更新
3. 显示计算数据
4. 更新状态
5. 删除键处理
6. data.cpp总代码
4. Calculate类:
(1)calculate.h
(2)calculate.cpp
5. Dialog (联系/对象/触发信号)
(1)dialog .h
1. 创建对象
2. 计算函数触发信号
3. 文本框显示处理函数
(2)dialog .cpp(联系及初始化)
1. 联系及其初始化
2. 显示计算数据(注意头文件)
6. Dialog(按键处理)
1. 按键转为槽方法
1.
2.
3.
2. 0~9数字按键处理
3. 运算符处理
4. 计算结果
5. 删除键
6. 清零键
7. 图像小按键
7. Dialog(总程序)
(1)dialog .h
(2)dialog .cpp
8. 主函数
五、总程序:
1. headers
1. Calculate.h
2. Data.h
3. Dialog.h
2. Soures
1. Calculate.cpp
2. Data.cpp
3. Dialog.cpp
4. main.cpp
六、windows可执行文件
1. 生成exe文件
2. exe文件存放
3. 找到
4. 此处需要准确(二 、7)
5. 执行 windeployqt exe文件目录
编辑
七、实现效果
一、题目描述:
创建一个简单的图形用户界面(GUI),包括一个文本框用于显示计算结果,以及数字键盘和运算符按钮。用户可以通过点击这些按钮输入数字和运算符,并查看计算结果。
实现计算功能,编写相应的代码逻辑。当用户输入两个数字和一个运算符时,程序会根据运算符执行相应的加、减、乘、除运算,并将结果显示在文本框中。简化:要求不包含小数,使用整数运算。
以简洁明了的方式呈现给用户,使用户可以轻松地进行基本的算术计算。尽量确保界面设计简单大方,操作直观,为用户提供良好的使用体验。
二、创建工程:
1.
2.
3.
4. 默认
5.
6.
7.
8. 默认
9. 创建完成
三、UI界面设计:
1. 添加按钮
1.
2. 按钮界面
3. 按钮绑定快捷键
2. 文本框添加
1. 文本框字体
2. 默认文本
3. 文本对齐方式
3. 添加图片
1. 找到图片并添加到工程目录下
2.
3.
4. 图片名称
5.
6.添加-->添加前缀
7. 改成一个下划线
7. 再次添加 -- > 添加文件,此次有 添加文件 了
8.选择图片文件
9. 成功添加文件 -->>> 运行一下
10. 成功
4. 按钮添加图片
1. -->> 选择资源
2. 选择对应的图片资源
3. 添加成功
5. UI界面大致样式
四、程序编写:
1. 思路:
用两个类来处理,一个data保存运算符,数字等。一个用于保存算式计算函数。
data类,中初始化数据,2为数据,一个运算符,一个状态数。
Calculate类:主一个运算函数,多个触发信号
2. 创建类:
1.
2.
3.
4.
5. 创建成功
6. 同上方式载创建一个 Calculate 类
3. data类:
(1)data.h
#ifndef DATA_H
#define DATA_H#include <QObject>//#include <dialog.h>class Data : public QObject
{Q_OBJECT
public:explicit Data(QObject *parent = nullptr);int num1;int num2;int state; //保存当前数据状态char operator_c; //运算符signals:public slots:void init_data();//初始化数据//int update_satate(int a,int b, int i); //根据状态改变数void update(int a); //将数据更新 比如原本是 1 按下 5 后通过此函数 变成 51;void upstate(int i); //更新状态 0更改num1 // 1更改char // 2 更改 num2void backspace(); //删除键void display_data(int i); //显示计算数据};#endif // DATA_H
(2)data.cpp
1. 初始化数据
void Data::init_data(){num1 = 0;num2 = 0;state = 0;operator_c = ' ';
}
2. 数据更新
根据不同状态,看保存到第一位数据还是第二位数据
void Data::update(int a){ //将数据更新 //比如原本是 1 按下 5 后通过此函数 变成 51;//状态更新处理if(state == 1){upstate(2);}if(state == 0) num1 = num1*10+a;else if(state == 2) num2 = num2*10+a;}
3. 显示计算数据
void Data::display_data(int i){ //显示计算数据if(i == 0){qDebug()<<num1<<" ";//<<operator_c<<" "<<num2;}else if( i == 1)qDebug()<<num1<<" "<<operator_c;//<<" "<<num2;else if( i == 2)qDebug()<<num1<<" "<<operator_c<<" "<<num2;elseqDebug()<<num1<<" ";//<<operator_c<<" "<<num2;qDebug()<<" ";}
4. 更新状态
void Data::upstate(int i){ //更新状态 0更改num1 // 1更改char // 2 更改 num2state = i;
}
5. 删除键处理
根据状态从最后一会数据开始删除。
6. data.cpp总代码
#include "data.h"#include "QDebug"
#pragma execution_character_set("utf-8")#include <QLineEdit>Data::Data(QObject *parent) : QObject(parent)
{}void Data::init_data(){num1 = 0;num2 = 0;state = 0;operator_c = ' ';
}void Data::update(int a){ //将数据更新 //比如原本是 1 按下 5 后通过此函数 变成 51;//状态更新处理if(state == 1){upstate(2);}if(state == 0) num1 = num1*10+a;else if(state == 2) num2 = num2*10+a;
}void Data::display_data(int i){ //显示计算数据if(i == 0){qDebug()<<num1<<" ";//<<operator_c<<" "<<num2;}else if( i == 1)qDebug()<<num1<<" "<<operator_c;//<<" "<<num2;else if( i == 2)qDebug()<<num1<<" "<<operator_c<<" "<<num2;elseqDebug()<<num1<<" ";//<<operator_c<<" "<<num2;qDebug()<<" ";}void Data::upstate(int i){ //更新状态 0更改num1 // 1更改char // 2 更改 num2state = i;
}void Data::backspace(){ //删除键if(state == 0){num1 = num1/10;}else if(state == 1){state = 0;}else if(state == 2){num2 = num2/10;if(num2 == 0) state = 1;}
}
4. Calculate类:
(1)calculate.h
#ifndef CALCULATE_H
#define CALCULATE_H#include <QObject>class Calculate : public QObject
{Q_OBJECT
public:explicit Calculate(QObject *parent = nullptr);signals:void signal_num(int a); //num 触发信号void display(int i); //计算机显示界面void backspace(); //删除键触发信号public slots:float operation(int num1, char c, int num2); //运算函数//float operation(float num1, char c, float num2); //运算函数
};#endif // CALCULATE_H
(2)calculate.cpp
#include "calculate.h"
#include <iostream>
#include "QDebug"
#pragma execution_character_set("utf-8")Calculate::Calculate(QObject *parent) : QObject(parent)
{}//float Calculate::operation(float num1, char c, float num2){ //运算函数
float Calculate::operation(int num1, char c, int num2){ //运算函数float num = 0;if(c == '+'){num = num1 + num2;}else if(c == '-'){num = num1 - num2;}else if(c == '*'){num = num1 * num2;}else if(c == '/'){num = static_cast<float>(num1) / num2; //static_cast<float>强制转换}else{qDebug()<<"运算符不在以知范围内";}return num;
}
5. Dialog (联系/对象/触发信号)
(1)dialog .h
1. 创建对象
Data *data = new Data; //创建数字Calculate *calculate = new Calculate; //创建运算函数
2. 计算函数触发信号
signals:void display_data_signals(int num1, char c, int num2, int state);//显示计算数据
3. 文本框显示处理函数
private slots:void display_data(int num1, char c, int num2, int state);//显示计算数据
(2)dialog .cpp(联系及初始化)
1. 联系及其初始化
data -> init_data(); //初始化数据connect(calculate, SIGNAL(signal_num(int)), data, SLOT(update(int))); //数据联系connect(calculate, SIGNAL(display(int)), data, SLOT(display_data(int))); //显示联系 终点显示connect(calculate, SIGNAL(backspace()), data, SLOT(backspace())); //删除键 -联系connect(this, SIGNAL(display_data_signals(int,char,int,int)), this, SLOT(display_data(int,char,int,int))); //文本框显示
2. 显示计算数据(注意头文件)
#include <cstdio>
#include <string>
#include <QLineEdit>void Dialog::display_data(int num1, char c, int num2, int state){//显示计算数据char num1_c[20];sprintf(num1_c, "%d", num1);char num2_c[20];sprintf(num2_c, "%d", num2);std::string result;//拼接字符串if(state == 0)result = std::to_string(num1);else if( state == 1)result = std::to_string(num1) + c;else if( state == 2)result = std::to_string(num1) + c + std::to_string(num2);elseresult = std::to_string(num1);ui->lineEdit->setText(result.c_str());
}
6. Dialog(按键处理)
1. 按键转为槽方法
1.
2.
3.
2. 0~9数字按键处理
此处调用 signal_num(i) i 为每次输入的数字,剩下两行显示在终端和文本框;
//按钮 0
void Dialog::on_pushButton_10_clicked()
{emit this -> calculate -> signal_num(0);emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}
其余0~9的按键通用,只是传入数字不一样
3. 运算符处理
其余减乘除同理 除非符合位不一样
//按钮 +
void Dialog::on_pushButton_13_clicked()
{if(data -> state == 2){ //如果前面已经满足等式要求this -> on_pushButton_12_clicked();//计算出结果 并将结果保存到num1}data -> operator_c = '+';data -> state = 1; //更新显示状态emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框}
4. 计算结果
//计算结果
void Dialog::on_pushButton_12_clicked()
{emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框//计算结果float num = calculate -> operation(data -> num1, data->operator_c, data -> num2);qDebug()<<" = "<<num;int num_n = num;//获取其整数值char num_c[20];if(data -> operator_c == '/')sprintf(num_c, "= %f", num);elsesprintf(num_c, "= %d", num_n);ui->lineEdit->setText(num_c);//将计算结果给num1;//data -> num1 = static_cast<int>(num); //static_cast<float>强制转换//data -> upstate(0);data -> init_data(); //从0开始 //清零data -> num1 = num_n;qDebug()<<" ";
}
5. 删除键
//删除键
void Dialog::on_pushButton_18_clicked()
{emit this -> calculate -> backspace(); //触发删除键emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}
6. 清零键
//清零
void Dialog::on_pushButton_17_clicked()
{data -> init_data(); //从0开始 //清零emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}
7. 图像小按键
void Dialog::on_pushButton_11_clicked()
{static int i = -1;i++;switch(i){case 0:{ui->lineEdit->setText("(๑°⌓°๑) ");qDebug()<<"被发现了!!!";break;}case 1:{ui->lineEdit->setText("˳⚆ɞ⚆˳ ");qDebug()<<"又发现了一个!!!";break;}case 2:{ui->lineEdit->setText("દ ᵕ̈ ૩ ");qDebug()<<"别找了别找了 !!!";break;}case 3:{ui->lineEdit->setText("(•̥́_•ૅू˳) ");qDebug()<<"投降投降-->> !!!";break;}case 4:{ui->lineEdit->setText("♪(゚▽^*)ノ⌒☆ ");qDebug()<<"啦~啦~啦 听歌吗 !!!";break;}case 5:{ui->lineEdit->setText("~⃝ ˶ʜᴀᴘᴘʏ ᴅᴀʏs");qDebug()<<" !!!";break;}default :{ui->lineEdit->setText("没图了,下班");i = -1;}}}
7. Dialog(总程序)
(1)dialog .h
#ifndef DIALOG_H
#define DIALOG_H#include <QDialog>
#include <data.h>
#include <calculate.h>
QT_BEGIN_NAMESPACEnamespace Ui { class Dialog; }
QT_END_NAMESPACEclass Dialog : public QDialog
{Q_OBJECTpublic:Dialog(QWidget *parent = nullptr);~Dialog();Data *data = new Data; //创建数字Calculate *calculate = new Calculate; //创建运算函数signals:void display_data_signals(int num1, char c, int num2, int state);//显示计算数据private slots:void display_data(int num1, char c, int num2, int state);//显示计算数据void on_pushButton_10_clicked();void on_pushButton_clicked();void on_pushButton_2_clicked();void on_pushButton_3_clicked();void on_pushButton_4_clicked();void on_pushButton_5_clicked();void on_pushButton_6_clicked();void on_pushButton_7_clicked();void on_pushButton_8_clicked();void on_pushButton_9_clicked();void on_pushButton_13_clicked();void on_pushButton_14_clicked();void on_pushButton_15_clicked();void on_pushButton_16_clicked();void on_pushButton_18_clicked();void on_pushButton_12_clicked();void on_pushButton_17_clicked();void on_pushButton_11_clicked();private:Ui::Dialog *ui;
};#endif // DIALOG_H
(2)dialog .cpp
#include "dialog.h"
#include "ui_dialog.h"
#include <iostream>
#include <QMessageBox> // 包含QMessageBox头文件
#include "QDebug"
#pragma execution_character_set("utf-8")#include <cstdio>
#include <string>
#include <QLineEdit>Dialog::Dialog(QWidget *parent): QDialog(parent), ui(new Ui::Dialog)
{ui->setupUi(this);data -> init_data(); //初始化数据connect(calculate, SIGNAL(signal_num(int)), data, SLOT(update(int))); //数据联系connect(calculate, SIGNAL(display(int)), data, SLOT(display_data(int))); //显示联系 终点显示connect(calculate, SIGNAL(backspace()), data, SLOT(backspace())); //删除键 -联系connect(this, SIGNAL(display_data_signals(int,char,int,int)), this, SLOT(display_data(int,char,int,int))); //文本框显示//按键已与键盘正常绑定 可直接键盘输入 空格清零//小数点有彩蛋}Dialog::~Dialog()
{delete ui;
}void Dialog::display_data(int num1, char c, int num2, int state){//显示计算数据char num1_c[20];sprintf(num1_c, "%d", num1);char num2_c[20];sprintf(num2_c, "%d", num2);std::string result;//拼接字符串if(state == 0)result = std::to_string(num1);else if( state == 1)result = std::to_string(num1) + c;else if( state == 2)result = std::to_string(num1) + c + std::to_string(num2);elseresult = std::to_string(num1);ui->lineEdit->setText(result.c_str());
}//按钮 0
void Dialog::on_pushButton_10_clicked()
{emit this -> calculate -> signal_num(0);emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 1
void Dialog::on_pushButton_clicked()
{emit this -> calculate -> signal_num(1);emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 2
void Dialog::on_pushButton_2_clicked()
{emit this -> calculate -> signal_num(2);emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 3
void Dialog::on_pushButton_3_clicked()
{emit this -> calculate -> signal_num(3);emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 4
void Dialog::on_pushButton_4_clicked()
{emit this -> calculate -> signal_num(4);emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 5
void Dialog::on_pushButton_5_clicked()
{emit this -> calculate -> signal_num(5);emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 6
void Dialog::on_pushButton_6_clicked()
{emit this -> calculate -> signal_num(6);emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 7
void Dialog::on_pushButton_7_clicked()
{emit this -> calculate -> signal_num(7);emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 8
void Dialog::on_pushButton_8_clicked()
{emit this -> calculate -> signal_num(8);emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 9
void Dialog::on_pushButton_9_clicked()
{emit this -> calculate -> signal_num(9);emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 +
void Dialog::on_pushButton_13_clicked()
{if(data -> state == 2){ //如果前面已经满足等式要求this -> on_pushButton_12_clicked();//计算出结果 并将结果保存到num1}data -> operator_c = '+';data -> state = 1; //更新显示状态emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框}//按钮 -
void Dialog::on_pushButton_14_clicked()
{if(data -> state == 2){ //如果前面已经满足等式要求this -> on_pushButton_12_clicked();//计算出结果 并将结果保存到num1}data -> operator_c = '-';data -> state = 1; //更新显示状态emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 *
void Dialog::on_pushButton_15_clicked()
{if(data -> state == 2){ //如果前面已经满足等式要求this -> on_pushButton_12_clicked();//计算出结果 并将结果保存到num1}data -> operator_c = '*';data -> state = 1; //更新显示状态emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 /
void Dialog::on_pushButton_16_clicked()
{if(data -> state == 2){ //如果前面已经满足等式要求 直接计算this -> on_pushButton_12_clicked();//计算出结果 并将结果保存到num1}data -> operator_c = '/';data -> state = 1; //更新显示状态emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//删除键
void Dialog::on_pushButton_18_clicked()
{emit this -> calculate -> backspace(); //触发删除键emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//计算结果
void Dialog::on_pushButton_12_clicked()
{emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框//计算结果float num = calculate -> operation(data -> num1, data->operator_c, data -> num2);qDebug()<<" = "<<num;int num_n = num;//获取其整数值char num_c[20];if(data -> operator_c == '/')sprintf(num_c, "= %f", num);elsesprintf(num_c, "= %d", num_n);ui->lineEdit->setText(num_c);//将计算结果给num1;//data -> num1 = static_cast<int>(num); //static_cast<float>强制转换//data -> upstate(0);data -> init_data(); //从0开始 //清零data -> num1 = num_n;qDebug()<<" ";
}//清零
void Dialog::on_pushButton_17_clicked()
{data -> init_data(); //从0开始 //清零emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}void Dialog::on_pushButton_11_clicked()
{static int i = -1;i++;switch(i){case 0:{ui->lineEdit->setText("(๑°⌓°๑) ");qDebug()<<"被发现了!!!";break;}case 1:{ui->lineEdit->setText("˳⚆ɞ⚆˳ ");qDebug()<<"又发现了一个!!!";break;}case 2:{ui->lineEdit->setText("દ ᵕ̈ ૩ ");qDebug()<<"别找了别找了 !!!";break;}case 3:{ui->lineEdit->setText("(•̥́_•ૅू˳) ");qDebug()<<"投降投降-->> !!!";break;}case 4:{ui->lineEdit->setText("♪(゚▽^*)ノ⌒☆ ");qDebug()<<"啦~啦~啦 听歌吗 !!!";break;}case 5:{ui->lineEdit->setText("~⃝ ˶ʜᴀᴘᴘʏ ᴅᴀʏs");qDebug()<<" !!!";break;}default :{ui->lineEdit->setText("没图了,下班");i = -1;}}}
8. 主函数
#include "dialog.h"#include <QApplication>
#include <QIcon>#include "QDebug"
#pragma execution_character_set("utf-8")#include <QApplication>
#include <QLabel>int main(int argc, char *argv[])
{QApplication a(argc, argv);Dialog w;w.show();w.setWindowTitle("0X78的计算机");w.setWindowIcon(QIcon(":/img/JSJ.jpg"));return a.exec();
}
五、总程序:
1. headers
1. Calculate.h
#ifndef CALCULATE_H
#define CALCULATE_H#include <QObject>class Calculate : public QObject
{Q_OBJECT
public:explicit Calculate(QObject *parent = nullptr);signals:void signal_num(int a); //num 触发信号void display(int i); //计算机显示界面void backspace(); //删除键触发信号public slots:float operation(int num1, char c, int num2); //运算函数//float operation(float num1, char c, float num2); //运算函数
};#endif // CALCULATE_H
2. Data.h
#ifndef DATA_H
#define DATA_H#include <QObject>//#include <dialog.h>class Data : public QObject
{Q_OBJECT
public:explicit Data(QObject *parent = nullptr);int num1;int num2;int state; //保存当前数据状态char operator_c; //运算符signals:public slots:void init_data();//初始化数据//int update_satate(int a,int b, int i); //根据状态改变数void update(int a); //将数据更新 比如原本是 1 按下 5 后通过此函数 变成 51;void upstate(int i); //更新状态 0更改num1 // 1更改char // 2 更改 num2void backspace(); //删除键void display_data(int i); //显示计算数据};#endif // DATA_H
3. Dialog.h
#ifndef DIALOG_H
#define DIALOG_H#include <QDialog>
#include <data.h>
#include <calculate.h>
QT_BEGIN_NAMESPACEnamespace Ui { class Dialog; }
QT_END_NAMESPACEclass Dialog : public QDialog
{Q_OBJECTpublic:Dialog(QWidget *parent = nullptr);~Dialog();Data *data = new Data; //创建数字Calculate *calculate = new Calculate; //创建运算函数signals:void display_data_signals(int num1, char c, int num2, int state);//显示计算数据private slots:void display_data(int num1, char c, int num2, int state);//显示计算数据void on_pushButton_10_clicked();void on_pushButton_clicked();void on_pushButton_2_clicked();void on_pushButton_3_clicked();void on_pushButton_4_clicked();void on_pushButton_5_clicked();void on_pushButton_6_clicked();void on_pushButton_7_clicked();void on_pushButton_8_clicked();void on_pushButton_9_clicked();void on_pushButton_13_clicked();void on_pushButton_14_clicked();void on_pushButton_15_clicked();void on_pushButton_16_clicked();void on_pushButton_18_clicked();void on_pushButton_12_clicked();void on_pushButton_17_clicked();void on_pushButton_11_clicked();private:Ui::Dialog *ui;
};#endif // DIALOG_H
2. Soures
1. Calculate.cpp
#include "calculate.h"
#include <iostream>
#include "QDebug"
#pragma execution_character_set("utf-8")Calculate::Calculate(QObject *parent) : QObject(parent)
{}//float Calculate::operation(float num1, char c, float num2){ //运算函数
float Calculate::operation(int num1, char c, int num2){ //运算函数float num = 0;if(c == '+'){num = num1 + num2;}else if(c == '-'){num = num1 - num2;}else if(c == '*'){num = num1 * num2;}else if(c == '/'){num = static_cast<float>(num1) / num2; //static_cast<float>强制转换}else{qDebug()<<"运算符不在以知范围内";}return num;
}
2. Data.cpp
#include "data.h"#include "QDebug"
#pragma execution_character_set("utf-8")#include <QLineEdit>Data::Data(QObject *parent) : QObject(parent)
{}void Data::init_data(){num1 = 0;num2 = 0;state = 0;operator_c = ' ';
}void Data::update(int a){ //将数据更新 //比如原本是 1 按下 5 后通过此函数 变成 51;//状态更新处理if(state == 1){upstate(2);}if(state == 0) num1 = num1*10+a;else if(state == 2) num2 = num2*10+a;
}void Data::display_data(int i){ //显示计算数据if(i == 0){qDebug()<<num1<<" ";//<<operator_c<<" "<<num2;}else if( i == 1)qDebug()<<num1<<" "<<operator_c;//<<" "<<num2;else if( i == 2)qDebug()<<num1<<" "<<operator_c<<" "<<num2;elseqDebug()<<num1<<" ";//<<operator_c<<" "<<num2;qDebug()<<" ";}void Data::upstate(int i){ //更新状态 0更改num1 // 1更改char // 2 更改 num2state = i;
}void Data::backspace(){ //删除键if(state == 0){num1 = num1/10;}else if(state == 1){state = 0;}else if(state == 2){num2 = num2/10;if(num2 == 0) state = 1;}
}
3. Dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include <iostream>
#include <QMessageBox> // 包含QMessageBox头文件
#include "QDebug"
#pragma execution_character_set("utf-8")#include <cstdio>
#include <string>
#include <QLineEdit>Dialog::Dialog(QWidget *parent): QDialog(parent), ui(new Ui::Dialog)
{ui->setupUi(this);data -> init_data(); //初始化数据connect(calculate, SIGNAL(signal_num(int)), data, SLOT(update(int))); //数据联系connect(calculate, SIGNAL(display(int)), data, SLOT(display_data(int))); //显示联系 终点显示connect(calculate, SIGNAL(backspace()), data, SLOT(backspace())); //删除键 -联系connect(this, SIGNAL(display_data_signals(int,char,int,int)), this, SLOT(display_data(int,char,int,int))); //文本框显示//按键已与键盘正常绑定 可直接键盘输入 空格清零//小数点有彩蛋}Dialog::~Dialog()
{delete ui;
}void Dialog::display_data(int num1, char c, int num2, int state){//显示计算数据char num1_c[20];sprintf(num1_c, "%d", num1);char num2_c[20];sprintf(num2_c, "%d", num2);std::string result;//拼接字符串if(state == 0)result = std::to_string(num1);else if( state == 1)result = std::to_string(num1) + c;else if( state == 2)result = std::to_string(num1) + c + std::to_string(num2);elseresult = std::to_string(num1);ui->lineEdit->setText(result.c_str());
}//按钮 0
void Dialog::on_pushButton_10_clicked()
{emit this -> calculate -> signal_num(0);emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 1
void Dialog::on_pushButton_clicked()
{emit this -> calculate -> signal_num(1);emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 2
void Dialog::on_pushButton_2_clicked()
{emit this -> calculate -> signal_num(2);emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 3
void Dialog::on_pushButton_3_clicked()
{emit this -> calculate -> signal_num(3);emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 4
void Dialog::on_pushButton_4_clicked()
{emit this -> calculate -> signal_num(4);emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 5
void Dialog::on_pushButton_5_clicked()
{emit this -> calculate -> signal_num(5);emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 6
void Dialog::on_pushButton_6_clicked()
{emit this -> calculate -> signal_num(6);emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 7
void Dialog::on_pushButton_7_clicked()
{emit this -> calculate -> signal_num(7);emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 8
void Dialog::on_pushButton_8_clicked()
{emit this -> calculate -> signal_num(8);emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 9
void Dialog::on_pushButton_9_clicked()
{emit this -> calculate -> signal_num(9);emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 +
void Dialog::on_pushButton_13_clicked()
{if(data -> state == 2){ //如果前面已经满足等式要求this -> on_pushButton_12_clicked();//计算出结果 并将结果保存到num1}data -> operator_c = '+';data -> state = 1; //更新显示状态emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框}//按钮 -
void Dialog::on_pushButton_14_clicked()
{if(data -> state == 2){ //如果前面已经满足等式要求this -> on_pushButton_12_clicked();//计算出结果 并将结果保存到num1}data -> operator_c = '-';data -> state = 1; //更新显示状态emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 *
void Dialog::on_pushButton_15_clicked()
{if(data -> state == 2){ //如果前面已经满足等式要求this -> on_pushButton_12_clicked();//计算出结果 并将结果保存到num1}data -> operator_c = '*';data -> state = 1; //更新显示状态emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//按钮 /
void Dialog::on_pushButton_16_clicked()
{if(data -> state == 2){ //如果前面已经满足等式要求 直接计算this -> on_pushButton_12_clicked();//计算出结果 并将结果保存到num1}data -> operator_c = '/';data -> state = 1; //更新显示状态emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//删除键
void Dialog::on_pushButton_18_clicked()
{emit this -> calculate -> backspace(); //触发删除键emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}//计算结果
void Dialog::on_pushButton_12_clicked()
{emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框//计算结果float num = calculate -> operation(data -> num1, data->operator_c, data -> num2);qDebug()<<" = "<<num;int num_n = num;//获取其整数值char num_c[20];if(data -> operator_c == '/')sprintf(num_c, "= %f", num);elsesprintf(num_c, "= %d", num_n);ui->lineEdit->setText(num_c);//将计算结果给num1;//data -> num1 = static_cast<int>(num); //static_cast<float>强制转换//data -> upstate(0);data -> init_data(); //从0开始 //清零data -> num1 = num_n;qDebug()<<" ";
}//清零
void Dialog::on_pushButton_17_clicked()
{data -> init_data(); //从0开始 //清零emit this -> calculate -> display(data -> state); //显示state下的emit this -> display_data_signals(data -> num1, data -> operator_c, data -> num2, data -> state); //将数据显示到文本框
}void Dialog::on_pushButton_11_clicked()
{static int i = -1;i++;switch(i){case 0:{ui->lineEdit->setText("(๑°⌓°๑) ");qDebug()<<"被发现了!!!";break;}case 1:{ui->lineEdit->setText("˳⚆ɞ⚆˳ ");qDebug()<<"又发现了一个!!!";break;}case 2:{ui->lineEdit->setText("દ ᵕ̈ ૩ ");qDebug()<<"别找了别找了 !!!";break;}case 3:{ui->lineEdit->setText("(•̥́_•ૅू˳) ");qDebug()<<"投降投降-->> !!!";break;}case 4:{ui->lineEdit->setText("♪(゚▽^*)ノ⌒☆ ");qDebug()<<"啦~啦~啦 听歌吗 !!!";break;}case 5:{ui->lineEdit->setText("~⃝ ˶ʜᴀᴘᴘʏ ᴅᴀʏs");qDebug()<<" !!!";break;}default :{ui->lineEdit->setText("没图了,下班");i = -1;}}}
4. main.cpp
#include "dialog.h"#include <QApplication>
#include <QIcon>#include "QDebug"
#pragma execution_character_set("utf-8")#include <QApplication>
#include <QLabel>int main(int argc, char *argv[])
{QApplication a(argc, argv);Dialog w;w.show();w.setWindowTitle("0X78的计算机");w.setWindowIcon(QIcon(":/img/JSJ.jpg"));return a.exec();
}
六、windows可执行文件
1. 生成exe文件
1.
2.再次运行就能生成exe
3. exe文件位置
2. exe文件存放
新建一个文件夹将exe文件放入,这个文件夹是保存exe所有文件的。
3. 找到
4. 此处需要准确(二 、7)
这里选择的为最初创建文件时,(二 、7)
5. 执行 windeployqt exe文件目录
比如:
运行完毕