目录
一、概述
1. 规划与设计
- 功能确定:决定计算器将支持哪些基本运算(加、减、乘、除、百分之、平方、开根号、变分数等)。
- 界面设计:设计用户界面,通常包括数字键、运算符键、清除键和显示结果的显示屏。
2. 环境搭建
- 安装Qt:确保已经安装了Qt开发环境和Qt Creator集成开发环境。
- 创建项目:在Qt Creator中创建一个新的Qt Widgets Application项目。
3. UI布局
- 使用Qt Designer:通过Qt Designer设计UI界面,包括按钮和显示屏。
- 布局管理:使用布局管理器(如QGridLayout)来排列按钮,确保界面在不同尺寸的窗口中也能正确显示。
4. 逻辑实现
- 定义槽函数:为每个按钮编写槽函数,用于处理点击事件。
- 信号与槽连接:将按钮的点击信号连接到对应的槽函数。
- 输入处理:在槽函数中处理用户输入,更新显示屏内容。
二、界面设计
给每个组件设置名称,在我设计的界面中组件用的是PublishButton按钮和QLable组件共同构成,其中将PublishButton都存入Widget组件中,QLable放入Widget中。(注意:组件名称不可重名)
界面效果
三、程序代码
1、程序代码存放位置
2、widget.h文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "QString"
#include "QIcon"
#include "math.h"
#include "QPixmap"
#include <QPainter>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void paintEvent(QPaintEvent *event);
public slots:
void bnt_num0();
void bnt_num1();
void bnt_num2();
void bnt_num3();
void bnt_num4();
void bnt_num5();
void bnt_num6();
void bnt_num7();
void bnt_num8();
void bnt_num9();
void bnt_add();
void bnt_sub();
void bnt_mul();
void bnt_div();
void bnt_d();
void bnt_over();
private slots:
void on_clear_clicked(bool checked);
void on_clearall_clicked(bool checked);
void on_delete_2_clicked(bool checked);
void on_fenshu_clicked(bool checked);
void on_pow_clicked(bool checked);
void on_genhao_clicked(bool checked);
void on_aors_clicked(bool checked);
void on_yu_clicked(bool checked);
private:
Ui::Widget *ui;
QString symbol = "";
QString num1 = "";
QString num2 = "";
QString over = "";
QString msg = "";
bool isSave01 = true;
bool isSave02 = true;
};
#endif // WIDGET_H
3、widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include "QIcon"
#include "QString"
#include "QDebug"
#include "QLabel"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setFixedSize(430,550);
this->setWindowTitle("计算器");
this->setWindowIcon(QIcon(":/image/computer.webp"));
//使用setStyleSheet方法设置标签颜色
ui->label_msg->setStyleSheet("QLabel { color: grey; }");
connect(ui->add,SIGNAL(clicked(bool)),this,SLOT(bnt_add()));
connect(ui->div,SIGNAL(clicked(bool)),this,SLOT(bnt_div()));
connect(ui->sub,SIGNAL(clicked(bool)),this,SLOT(bnt_sub()));
connect(ui->mul,SIGNAL(clicked(bool)),this,SLOT(bnt_mul()));
connect(ui->over,SIGNAL(clicked(bool)),this,SLOT(bnt_over()));
connect(ui->btn_d,SIGNAL(clicked(bool)),this,SLOT(bnt_d()));
connect(ui->btn_0,SIGNAL(clicked(bool)),this,SLOT(bnt_num0()));
connect(ui->btn_1,SIGNAL(clicked(bool)),this,SLOT(bnt_num1()));
connect(ui->btn_2,SIGNAL(clicked(bool)),this,SLOT(bnt_num2()));
connect(ui->btn_3,SIGNAL(clicked(bool)),this,SLOT(bnt_num3()));
connect(ui->btn_4,SIGNAL(clicked(bool)),this,SLOT(bnt_num4()));
connect(ui->btn_5,SIGNAL(clicked(bool)),this,SLOT(bnt_num5()));
connect(ui->btn_6,SIGNAL(clicked(bool)),this,SLOT(bnt_num6()));
connect(ui->btn_7,SIGNAL(clicked(bool)),this,SLOT(bnt_num7()));
connect(ui->btn_8,SIGNAL(clicked(bool)),this,SLOT(bnt_num8()));
connect(ui->btn_9,SIGNAL(clicked(bool)),this,SLOT(bnt_num9()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
// painter.drawPixmap(0,0,width(),height(),QPixmap(":/image/bizhi.jpg"));
}
//数字0代码段
void Widget::bnt_num0()
{
if(symbol == "")
{
if(num1 == "0")
{
num1.chop(1);
}
num1.append("0");
ui->label_idet->setText(num1);
}
else
{
if(num2 == "0")
{
num2.chop(1);
}
num2.append("0");
ui->label_idet->setText(num2);
}
}
//数字1代码段
void Widget::bnt_num1()
{
if(symbol == "")
{
if(num1 == "0")
{
num1.chop(1);
}
num1.append("1");
ui->label_idet->setText(num1);
}
else
{
if(num2 == "0")
{
num2.chop(1);
}
num2.append("1");
ui->label_idet->setText(num2);
}
}
//数字2代码段
void Widget::bnt_num2()
{
if(symbol == "")
{
if(num1 == "0")
{
num1.chop(1);
}
num1.append("2");
ui->label_idet->setText(num1);
}
else
{
if(num2 == "0")
{
num2.chop(1);
}
num2.append("2");
ui->label_idet->setText(num2);
}
}
//数字3代码段
void Widget::bnt_num3()
{
if(symbol == "")
{
if(num1 == "0")
{
num1.chop(1);
}
num1.append("3");
ui->label_idet->setText(num1);
}
else
{
if(num2 == "0")
{
num2.chop(1);
}
num2.append("3");
ui->label_idet->setText(num2);
}
}
//数字4代码段
void Widget::bnt_num4()
{
if(symbol == "")
{
if(num1 == "0")
{
num1.chop(1);
}
num1.append("4");
ui->label_idet->setText(num1);
}
else
{
if(num2 == "0")
{
num2.chop(1);
}
num2.append("4");
ui->label_idet->setText(num2);
}
}
//数字5代码段
void Widget::bnt_num5()
{
if(symbol == "")
{
if(num1 == "0")
{
num1.chop(1);
}
num1.append("5");
ui->label_idet->setText(num1);
}
else
{
if(num2 == "0")
{
num2.chop(1);
}
num2.append("5");
ui->label_idet->setText(num2);
}
}
//数字6代码段
void Widget::bnt_num6()
{
if(symbol == "")
{
if(num1 == "0")
{
num1.chop(1);
}
num1.append("6");
ui->label_idet->setText(num1);
}
else
{
if(num2 == "0")
{
num2.chop(1);
}
num2.append("6");
ui->label_idet->setText(num2);
}
}
//数字7代码段
void Widget::bnt_num7()
{
if(symbol == "")
{
if(num1 == "0")
{
num1.chop(1);
}
num1.append("7");
ui->label_idet->setText(num1);
}
else
{
if(num2 == "0")
{
num2.chop(1);
}
num2.append("7");
ui->label_idet->setText(num2);
}
}
//数字8代码段
void Widget::bnt_num8()
{
if(symbol == "")
{
if(num1 == "0")
{
num1.chop(1);
}
num1.append("8");
ui->label_idet->setText(num1);
}
else
{
if(num2 == "0")
{
num2.chop(1);
}
num2.append("8");
ui->label_idet->setText(num2);
}
}
//数字9代码段
void Widget::bnt_num9()
{
if(symbol == "")
{
if(num1 == "0")
{
num1.chop(1);
}
num1.append("9");
ui->label_idet->setText(num1);
}
else
{
if(num2 == "0")
{
num2.chop(1);
}
num2.append("9");
ui->label_idet->setText(num2);
}
}
//小数点代码段
void Widget::bnt_d()
{
if(symbol == "")
{
num1.append(".");
ui->label_idet->setText(num1);
}
else
{
num2.append(".");
ui->label_idet->setText(num2);
}
}
//加法
void Widget::bnt_add()
{
double n1 = num1.toDouble();
double n2 = num2.toDouble();
double x = 0.0;
if(symbol == "+")
{
//计算三位以上的式子
x = n1 + n2;
num1 = QString::number(x);
qDebug()<<"x="<<x;
}
else if (symbol =="-")
{
x = n1 - n2;
num1 = QString::number(x);
}
else if (symbol =="×")
{
x = n1 * n2;
num1 = QString::number(x);
}
else if (symbol =="÷")
{
x = n1 / n2;
num1 = QString::number(x);
}
else if(symbol == "%")
{
if(num1 == "")
{
ui->label_msg->setText("0");
}
else if(num2 != "")
{
num1 = QString::number(x);
}
}
num2 = "";
msg = "";
//将式子放入信息栏中
if(isSave01)
{
msg.append(num1);
}
symbol = "+";
msg.append(symbol);
ui->label_msg->setText(msg);
isSave01 = true;
}
//减法
void Widget::bnt_sub()
{
double n1 = num1.toDouble();
double n2 = num2.toDouble();
double x = 0.0;
if(symbol == "-")
{
//计算三位以上的式子
x = n1 - n2;
num1 = QString::number(x);
qDebug()<<"x="<<x;
}
else if (symbol =="+")
{
x = n1 + n2;
num1 = QString::number(x);
}
else if (symbol =="×")
{
x = n1 * n2;
num1 = QString::number(x);
}
else if (symbol =="÷")
{
x = n1 / n2;
num1 = QString::number(x);
}
else if(symbol == "%")
{
if(num1 == "")
{
ui->label_msg->setText("0");
}
else if(num2 != "")
{
num1 = QString::number(x);
}
}
num2 = "";
msg = "";
//将式子放入信息栏中
if(isSave01)
{
msg.append(num1);
}
symbol = "-";
msg.append(symbol);
ui->label_msg->setText(msg);
isSave01 = true;
}
//乘法
void Widget::bnt_mul()
{
double n1 = num1.toDouble();
double n2 = num2.toDouble();
double x = 0.0;
if(symbol == "×")
{
//计算三位以上的式子
x = n1 * n2;
num1 = QString::number(x);
qDebug()<<"x="<<x;
}
else if (symbol =="-")
{
x = n1 - n2;
num1 = QString::number(x);
}
else if (symbol =="+")
{
x = n1 + n2;
num1 = QString::number(x);
}
else if (symbol =="÷")
{
x = n1 / n2;
num1 = QString::number(x);
}
else if(symbol == "%")
{
if(num1 == "")
{
ui->label_msg->setText("0");
}
else if(num2 != "")
{
num1 = QString::number(x);
}
}
num2 = "";
msg = "";
//将式子放入信息栏中
if(isSave01)
{
msg.append(num1);
}
symbol = "×";
msg.append(symbol);
ui->label_msg->setText(msg);
isSave01 = true;
}
//除法
void Widget::bnt_div()
{
double n1 = num1.toDouble();
double n2 = num2.toDouble();
double x = 0.0;
if(symbol == "÷")
{
//计算三位以上的式子
x = n1 / n2;
num1 = QString::number(x);
qDebug()<<"x="<<x;
}
else if (symbol =="-")
{
x = n1 - n2;
num1 = QString::number(x);
}
else if (symbol =="×")
{
x = n1 * n2;
num1 = QString::number(x);
}
else if (symbol =="+")
{
x = n1 + n2;
num1 = QString::number(x);
}
else if(symbol == "%")
{
if(num1 == "")
{
ui->label_msg->setText("0");
}
else if(num2 != "")
{
x = n1 / n2;
num1 = QString::number(x);
}
}
num2 = "";
msg = "";
//将式子放入信息栏中
if(isSave01)
{
msg.append(num1);
}
symbol = "÷";
msg.append(symbol);
ui->label_msg->setText(msg);
isSave01 = true;
}
//等于
void Widget::bnt_over()
{
if(isSave02)
{
msg.append(num2);
}
double n1 = num1.toDouble();
double n2 = num2.toDouble();
double x = 0;
if(symbol == "+")
{
//计算按下等于之后的结果
x = n1 + n2;
}
else if(symbol == "-")
{
x = n1 - n2;
}
else if(symbol == "×")
{
x = n1 * n2;
}
else if(symbol == "÷")
{
if (n2 != 0)
{
x = n1 / n2;
}
else
{
ui->label_idet->setText("除数不能为0");
return;
}
}
else if(symbol == "%")
{
//判断num1存在的同时num2也存在
if(num1 == "")
{
ui->label_msg->setText("0");
}
else if(num2 != "")
{
}
}
else if(symbol == "")
{
x = n1;
}
msg.append("=");
ui->label_msg->setText(msg);
QString str = QString::number(x);
ui->label_idet->setText(str);
num1 = str;
num2 = "";
symbol = "";
msg = "";
isSave02 = true;
}
void Widget::on_clear_clicked(bool checked)
{
//删除最近一项
if(symbol == "")
{
num1 = "";
ui->label_idet->setText("0");
}
else
{
num2 = "";
ui->label_idet->setText("0");
}
}
void Widget::on_clearall_clicked(bool checked)
{
//删除所有项
num1 = "";
num2 = "";
symbol = "";
ui->label_idet->setText("0");
msg = "";
ui->label_msg->clear();
}
void Widget::on_delete_2_clicked(bool checked)
{
//删除一个数字
if(symbol == "")
{
num1.chop(1);
ui->label_idet->setText(num1);
}
else
{
num2.chop(1);
ui->label_idet->setText(num2);
}
}
void Widget::on_fenshu_clicked(bool checked)
{
//化为分数
if(num1 == 0)
{
ui->label_idet->setText("分母不能为0");
}
else
{
if(symbol == "")
{
double n1 = num1.toDouble();
msg.append("1/(");
msg.append(QString::number(n1));
msg.append(")");
ui->label_msg->setText(msg);
n1 = 1 / num1.toDouble();
QString str = QString::number(n1);
num1 = str;
ui->label_idet->setText(str);
}
else
{
double n2 = num2.toDouble();
isSave02=false;
msg.append("1/(");
msg.append(QString::number(n2));
msg.append(")");
ui->label_msg->setText(msg);
n2 = sqrt(num2.toDouble());
qDebug() << "n2:" << n2 << endl;
QString str = QString::number(n2);
num2 = str;
ui->label_idet->setText(str);
}
}
}
void Widget::on_pow_clicked(bool checked)
{
//平方
if(symbol == "")
{
double n1 = num1.toDouble();
msg.append("sqr(");
msg.append(QString::number(n1));
msg.append(")");
ui->label_msg->setText(msg);
n1 = num1.toDouble() * num1.toDouble();
QString str = QString::number(n1);
num1 = str;
ui->label_idet->setText(str);
}
else
{
double n2 = num2.toDouble();
isSave02=false;
msg.append("sqr(");
msg.append(QString::number(n2));
msg.append(")");
ui->label_msg->setText(msg);
n2 = num2.toDouble() * num2.toDouble();
qDebug() << "n2:" << n2 << endl;
QString str = QString::number(n2);
num2 = str;
ui->label_idet->setText(str);
}
}
void Widget::on_genhao_clicked(bool checked)
{
//开根号
if(symbol == "")
{
double n1 = num1.toDouble();
msg.append("根号(");
msg.append(QString::number(n1));
msg.append(")");
ui->label_msg->setText(msg);
n1 = sqrt(num1.toDouble());
QString str = QString::number(n1);
num1 = str;
ui->label_idet->setText(str);
}
else
{
double n2 = num2.toDouble();
isSave02=false;
msg.append("根号(");
msg.append(QString::number(n2));
msg.append(")");
ui->label_msg->setText(msg);
n2 = sqrt(num2.toDouble());
qDebug() << "n2:" << n2 << endl;
QString str = QString::number(n2);
num2 = str;
ui->label_idet->setText(str);
}
}
void Widget::on_aors_clicked(bool checked)
{
//转换正负数
if(symbol == "")
{
double n1 = num1.toDouble();
n1 = -n1;
num1 = QString::number(n1);
ui->label_idet->setText(num1);
qDebug() << "符号1:" << num1;
}
else
{
double n2 = num2.toDouble();
n2 = -n2;
num2 = QString::number(n2);
ui->label_idet->setText(num2);
qDebug() << "符号2:" << num2;
}
}
void Widget::on_yu_clicked(bool checked)//有问题
{
//72 + 5% =3.6
//72的5%是3.6,所以72+3.6就是75.6
//将式子放入信息栏中
double n1 = num1.toDouble();
double n2 = num2.toDouble();
double x;
if(num1 != "" && num2 != "")
{
n2 = (n2 / 100) * n1 ;
num2 = QString::number(n2);
qDebug() << "n2 = "<<n2;
}
else if(num2 != "")
{
ui->label_msg->setText("0");
}
if(symbol == "÷")
{
//计算三位以上的式子
x = n1 / n2;
num1 = QString::number(x);
qDebug()<<"x="<<x;
}
else if (symbol =="-")
{
x = n1 - n2;
num1 = QString::number(x);
}
else if (symbol =="×")
{
x = n1 * n2;
num1 = QString::number(x);
}
else if (symbol =="+")
{
x = n1 + n2;
num1 = QString::number(x);
}
msg.append(num2);
num2 = "";
}
4、main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
5、.pro文件
#-------------------------------------------------
#
# Project created by QtCreator 2024-07-15T19:27:26
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = computer
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
FORMS += widget.ui
RESOURCES += \
res.qrc
6、资源文件的添加
添加前缀为“ / ”
添加完成之后,点击添加文件,选择照片,保存。
标签:num1,num2,setText,C++,label,ui,计算器,msg,Qt From: https://blog.csdn.net/weixin_68995006/article/details/140603780