头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include<QDebug>
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
// void hello(int b);
signals:// zi ding yi xin hao
//void asignal(int a);
private slots:
void on_actionAa2_triggered();
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_9_clicked();
void on_pushButton_7_clicked();
void on_pushButton_8_clicked();
void on_pushButton_12_clicked();
void on_pushButton_11_clicked();
void on_pushButton_10_clicked();
void on_pushButton_14_clicked();
void on_pushButton_15_clicked();
void on_pushButton_13_clicked();
void on_pushButton_16_clicked();
void on_pushButton_17_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
主函数
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
int step=1;//判断是第几个数
int num1=0;
int num2=0;
int sig=0;//1+2-3*4/加减乘除判断变量
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//connect(this,SIGNAL(asignal(int)),this,SLOT(hello(int)));//这是另一个题的东西,自定义信号和传参信号
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionAa2_triggered()
{
close();
}
void MainWindow::on_pushButton_clicked()//点击1时下面雷同
{
QString newText;
QString str = ui->lineEdit->text();
newText="1";
str = str+newText;
ui->lineEdit->setText(str);
if(step==1)
num1=num1*10+1;
else
num2=num2*10+1;
}
void MainWindow::on_pushButton_2_clicked()
{
QString newText;
QString str = ui->lineEdit->text();
newText="2";
str = str+newText;
ui->lineEdit->setText(str);
if(step==1)
num1=num1*10+2;
else
num2=num2*10+2;
}
void MainWindow::on_pushButton_3_clicked()
{
QString newText;
QString str = ui->lineEdit->text();
newText="3";
str = str+newText;
ui->lineEdit->setText(str);
if(step==1)
num1=num1*10+3;
else
num2=num2*10+3;
}
void MainWindow::on_pushButton_4_clicked()
{
QString newText;
QString str = ui->lineEdit->text();
newText="4";
str = str+newText;
ui->lineEdit->setText(str);
if(step==1)
num1=num1*10+4;
else
num2=num2*10+4;
}
void MainWindow::on_pushButton_5_clicked()
{
QString newText;
QString str = ui->lineEdit->text();
newText="5";
str = str+newText;
ui->lineEdit->setText(str);
if(step==1)
num1=num1*10+5;
else
num2=num2*10+5;
}
void MainWindow::on_pushButton_6_clicked()
{
QString newText;
QString str = ui->lineEdit->text();
newText="6";
str = str+newText;
ui->lineEdit->setText(str);
if(step==1)
num1=num1*10+6;
else
num2=num2*10+6;
}
void MainWindow::on_pushButton_9_clicked()
{
QString newText;
QString str = ui->lineEdit->text();
newText="7";
str = str+newText;
ui->lineEdit->setText(str);
if(step==1)
num1=num1*10+7;
else
num2=num2*10+7;
}
void MainWindow::on_pushButton_7_clicked()
{
QString newText;
QString str = ui->lineEdit->text();
newText="8";
str = str+newText;
ui->lineEdit->setText(str);
if(step==1)
num1=num1*10+8;
else
num2=num2*10+8;
}
void MainWindow::on_pushButton_8_clicked()
{
QString newText;
QString str = ui->lineEdit->text();
newText="9";
str = str+newText;
ui->lineEdit->setText(str);
if(step==1)
num1=num1*10+9;
else
num2=num2*10+9;
}
void MainWindow::on_pushButton_12_clicked()//++++++++
{
QString newText;
QString str = ui->lineEdit->text();
newText="+";
str = str+newText;
ui->lineEdit->setText(str);
step++;
sig=1;
}
void MainWindow::on_pushButton_11_clicked()//======== =号
{
if(step==2)
{\
if(sig==1)
{
num1=num1+num2;
}
if(sig==2)
{
num1=num1-num2;
}
if(sig==3)
{
num1=num1*num2;
}
if(sig==4)
{
num1=num1/num2;
}
QString newText;
QString str = ui->lineEdit->text();
newText="=";
QString temp =QString::number(num1,10);//把数字打印到文本框
str = str+newText+temp;
ui->lineEdit->setText(str);
}
}
void MainWindow::on_pushButton_10_clicked()//_______减号
{
QString newText;
QString str = ui->lineEdit->text();
newText="-";
str = str+newText;
ui->lineEdit->setText(str);
step++;
sig=2;
}
void MainWindow::on_pushButton_14_clicked()//*********
{
QString newText;
QString str = ui->lineEdit->text();
newText="x";
str = str+newText;
ui->lineEdit->setText(str);
step++;
sig=3;
}
void MainWindow::on_pushButton_15_clicked()
{
QString newText;
QString str = ui->lineEdit->text();
newText="/";
str = str+newText;
ui->lineEdit->setText(str);
step++;
sig=4;
}
void MainWindow::on_pushButton_13_clicked()
{
QString newT="\0";
ui->lineEdit->setText(newT);
step=1;
num1=0,num2=0;
sig=0;
}
void MainWindow::on_pushButton_16_clicked()
{
close();
}
void MainWindow::on_pushButton_17_clicked()//0
{
QString newText;
QString str = ui->lineEdit->text();
newText="0";
str = str+newText;
ui->lineEdit->setText(str);
if(step==1)
num1=num1*10;
else
num2=num2*10;
}