Qt Demo—简单的四则运算计算器
程序员文章站
2022-05-06 13:00:36
...
项目功能
- 使用QT实现一个最简单的四则运算计算器
- 暂时只能实现单目运算
项目界面
项目文件
MainWindow类
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "model.h"
#include <QString>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots: //槽函数
void getBtn_0();
void getBtn_1();
void getBtn_2();
void getBtn_3();
void getBtn_4();
void getBtn_5();
void getBtn_6();
void getBtn_7();
void getBtn_8();
void getBtn_9();
void on_btn_add_clicked();
void on_btn_minus_clicked();
void on_btn_multiply_clicked();
void on_btn_divide_clicked();
void on_btn_equal_clicked();
void on_btn_clear_clicked();
private:
Ui::MainWindow *ui; //界面对象
model *model1; //模型对象
QString temp; //暂时存放数字字符串
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->model1 = new model;
this->temp = "";
//connect连接信号与槽
connect(this->ui->btn_0, SIGNAL(clicked(bool)), this, SLOT(getBtn_0()));
connect(this->ui->btn_1, SIGNAL(clicked(bool)), this, SLOT(getBtn_1()));
connect(this->ui->btn_2, SIGNAL(clicked(bool)), this, SLOT(getBtn_2()));
connect(this->ui->btn_3, SIGNAL(clicked(bool)), this, SLOT(getBtn_3()));
connect(this->ui->btn_4, SIGNAL(clicked(bool)), this, SLOT(getBtn_4()));
connect(this->ui->btn_5, SIGNAL(clicked(bool)), this, SLOT(getBtn_5()));
connect(this->ui->btn_6, SIGNAL(clicked(bool)), this, SLOT(getBtn_6()));
connect(this->ui->btn_7, SIGNAL(clicked(bool)), this, SLOT(getBtn_7()));
connect(this->ui->btn_8, SIGNAL(clicked(bool)), this, SLOT(getBtn_8()));
connect(this->ui->btn_9, SIGNAL(clicked(bool)), this, SLOT(getBtn_9()));
//运算符使用“转到槽”机制
}
MainWindow::~MainWindow()
{
delete ui;
}
//非bool类型:获取:属性名本身;设置:set+属性名
void MainWindow::getBtn_0()
{
if(this->temp != ""){
temp += this->ui->btn_0->text();
this->ui->lbl_display->setText(temp);
}else{
}
}
void MainWindow::getBtn_1()
{
temp += this->ui->btn_1->text();
this->ui->lbl_display->setText(temp);
}
void MainWindow::getBtn_2()
{
temp += this->ui->btn_2->text();
this->ui->lbl_display->setText(temp);
}
void MainWindow::getBtn_3()
{
temp += this->ui->btn_3->text();
this->ui->lbl_display->setText(temp);
}
void MainWindow::getBtn_4()
{
temp += this->ui->btn_4->text();
this->ui->lbl_display->setText(temp);
}
void MainWindow::getBtn_5()
{
temp += this->ui->btn_5->text();
this->ui->lbl_display->setText(temp);
}
void MainWindow::getBtn_6()
{
temp += this->ui->btn_6->text();
this->ui->lbl_display->setText(temp);
}
void MainWindow::getBtn_7()
{
temp += this->ui->btn_7->text();
this->ui->lbl_display->setText(temp);
}
void MainWindow::getBtn_8()
{
temp += this->ui->btn_8->text();
this->ui->lbl_display->setText(temp);
}
void MainWindow::getBtn_9()
{
temp += this->ui->btn_9->text();
this->ui->lbl_display->setText(temp);
}
//运算符使用“转到槽”机制
void MainWindow::on_btn_add_clicked()
{
//将temp储存到num1
this->model1->setNum1(this->temp.toInt());
temp = "";
this->model1->setFlag("+");
this->ui->lbl_display->setText("+");
}
void MainWindow::on_btn_minus_clicked()
{
this->model1->setNum1(this->temp.toInt());
this->model1->setFlag("-");
this->ui->lbl_display->setText("-");
temp = "";
}
void MainWindow::on_btn_multiply_clicked()
{
this->model1->setNum1(this->temp.toInt());
this->model1->setFlag("*");
this->ui->lbl_display->setText("×");
temp = "";
}
void MainWindow::on_btn_divide_clicked()
{
this->model1->setNum1(this->temp.toInt());
this->model1->setFlag("/");
this->ui->lbl_display->setText("÷");
temp = "";
}
void MainWindow::on_btn_equal_clicked()
{
this->model1->setNum2(this->temp.toInt());
this->ui->lbl_display->setText(model1->doExpr());
}
void MainWindow::on_btn_clear_clicked()
{
this->model1->setNum1(0);
this->model1->setNum2(0);
this->model1->setFlag("");
temp = "";
this->ui->lbl_display->setText("0");
}
Model类
model.h
#ifndef MODEL_H
#define MODEL_H
#include <QString>
// num1 +-*/ num2
class model
{
public:
model();
void setNum1(int num);
void setNum2(int num);
void setFlag(QString flag);
QString doExpr(); //计算结果
private:
int num1, num2;
QString flag;
//int result;
};
#endif // MODEL_H
model.cpp
#include "model.h"
model::model()
{
this->num1 = 0;
this->num2 = 0;
}
void model::setNum1(int num)
{
this->num1 = num;
}
void model::setNum2(int num)
{
this->num2 = num;
}
void model::setFlag(QString flag)
{
this->flag = flag;
}
QString model::doExpr()
{
int result = 0;
if(this->flag == "+"){
result = num1 + num2;
}else if(this->flag == '-'){
result = num1 - num2;
}else if(this->flag == "*"){
result = num1 * num2;
}else if(this->flag == "/"){
if(num2 == 0){
return "ERROR";
}else{
result = num1 / num2;
}
}else{
return QString::number(num1);
}
return QString::number(result);
}
Main类
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,9,0))
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "model.h"
#include <QString>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots: //槽函数
void getBtn_0();
void getBtn_1();
void getBtn_2();
void getBtn_3();
void getBtn_4();
void getBtn_5();
void getBtn_6();
void getBtn_7();
void getBtn_8();
void getBtn_9();
void on_btn_add_clicked();
void on_btn_minus_clicked();
void on_btn_multiply_clicked();
void on_btn_divide_clicked();
void on_btn_equal_clicked();
void on_btn_clear_clicked();
private:
Ui::MainWindow *ui; //界面对象
model *model1; //模型对象
QString temp; //暂时存放数字字符串
};
#endif // MAINWINDOW_H
下一篇: 写给分布式神器Fourinone