代码如下
week-05.pro
#-------------------------------------------------
#
# Project created by QtCreator 2022-04-23T19:17:39
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = week-05
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has 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 \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QMainWindow *parent = 0);
~MainWindow();
signals:
void sig_caladd(int, int);
void sig_calsubtraction(int, int);
public slots:
void slot_caladd();//点击+处理函数
void slot_calsubtraction();//点击-处理函数
void slot_calcheng();//点击*处理函数
void slot_calchu();//点击/处理函数
void slot_caldel();//点击C处理函数
void slot_calout();//点击=处理函数
void slot_cal0();//点击1处理函数
void slot_cal1();//点击1处理函数
void slot_cal2();//点击2处理函数
void slot_cal3();//点击3处理函数
void slot_cal4();//点击4处理函数
void slot_cal5();//点击5处理函数
void slot_cal6();//点击6处理函数
void slot_cal7();//点击7处理函数
void slot_cal8();//点击8处理函数
void slot_cal9();//点击9处理函数
private:
Ui::MainWindow *ui;
int flag; //记录加减运算符
int flag1;//记录乘除运算符号
double temp;//存储输出的数字
double num; //运算结果
double numLeft;//存储加减左数
double numRight;//存储加减右数
char signLeft;//加减运算符
char signRight;//乘除运算符
QString output; //屏幕显示内容
};
#endif // WIDGET_H
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QMainWindow *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
this->flag=0;//运算符标记;
this->flag1 = 0;
this->temp = 0;
this->numLeft=0;//初始化
this->numRight=0;
this->signLeft='+';
this->signRight='+';
this->output="";//字符串用于在计算器界面显示
ui->setupUi(this);
//点击+ - * / Ce =
connect(ui->pushButton_sum,SIGNAL(clicked(bool)),this,SLOT(slot_caladd()));
connect(ui->pushButton_reduce,SIGNAL(clicked(bool)),this,SLOT(slot_calsubtraction()));
connect(ui->pushButton_cheng,SIGNAL(clicked(bool)),this,SLOT(slot_calcheng()));
connect(ui->pushButton_chu,SIGNAL(clicked(bool)),this,SLOT(slot_calchu()));
connect(ui->pushButton_ce,SIGNAL(clicked(bool))