//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QDebug>
class Widget : public QWidget
{
Q_OBJECT
public slots:
//按键捕捉
void num_pushed(){
QPushButton *digit = static_cast<QPushButton *>(sender());
qDebug() << digit->text();
QString data = le->text();
le->setText(data + digit->text());
}
//操作捕捉
void ope_pushed(){
QPushButton * op = static_cast<QPushButton *>(sender());
qDebug() << op->text();
Operate = op->text();
QString data = le->text();
data1 = data.toDouble();
le->setText(data+op->text());
}
// 小数点
void dot(){
QPushButton *dots = static_cast<QPushButton *>(sender());
qDebug() <<dots->text();
QString data = le->text();
le->setText(data+dots->text());
}
//等号
void cal(){
QPushButton *den = static_cast<QPushButton *>(sender());
qDebug() << den->text();
QString data = le->text();
int start,length;
for(int k1 = 0;k1<data.length();k1++){
if (data[k1] != '.' && !isdigit(data[k1].toLatin1())){
start = k1;break;
}
}
length = data.length()-1;
QString result = data.mid(start,length);
data2 = result.toDouble();
double sum;
switch (Operate.toStdString().c_str()[0]){
case '+': sum = data1 + data2; break;
case '-': sum = data1 - data2; break;
case '/': sum = data1 / data2; break;
case '*': sum = data1 * data2; break;
}
le->clear();
le->setText(QString::number(sum));
}
//退位
void tuiwei(){
QPushButton *den = static_cast<QPushButton *>(sender());
qDebug() << den->text();
QString data = le->text();
data.chop(1);
le->setText(data);
}
// 清除
void Clear(){
QPushButton *C = static_cast<QPushButton *>(sender());
qDebug() << C->text();
le->clear();
}
public:
Widget(QWidget *parent = 0);
~Widget();
QPushButton *numbt[10];
QPushButton *addbt,*subbt,*mulbt,*debt,*dotbt,*calbt,*delbt,*clearbt;
QLineEdit *le;
QString Operate;
double data1,data2;
};
#endif // WIDGET_H
// widget.cpp
#include "widget.h"
#include <QHBoxLayout>
#include <QGridLayout>
#include <QVBoxLayout>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
// 控件部分
le = new QLineEdit;
le->setAlignment(Qt::AlignRight);
delbt = new QPushButton("<");
addbt = new QPushButton("+");
subbt = new QPushButton("-");
mulbt = new QPushButton("*");
debt = new QPushButton("/");
dotbt = new QPushButton(".");
calbt = new QPushButton("=");
calbt->setMinimumHeight(63);
clearbt = new QPushButton("C");
for(int k1 = 0;k1<10;k1++){
numbt[k1] = new QPushButton(QString::number(k1));
}
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addWidget(le);
QGridLayout *grid = new QGridLayout;
grid->addWidget(numbt[7],0,0,1,1);
grid->addWidget(numbt[8],0,1,1,1);
grid->addWidget(numbt[9],0,2,1,1);
grid->addWidget(debt,0,3,1,1);
grid->addWidget(clearbt,0,4,1,1);
grid->addWidget(numbt[4],1,0,1,1);
grid->addWidget(numbt[5],1,1,1,1);
grid->addWidget(numbt[6],1,2,1,1);
grid->addWidget(mulbt,1,3,1,1);
grid->addWidget(delbt,1,4,1,1);
grid->addWidget(numbt[1],2,0,1,1);
grid->addWidget(numbt[2],2,1,1,1);
grid->addWidget(numbt[3],2,2,1,1);
grid->addWidget(subbt,2,3,1,1);
grid->addWidget(calbt,2,4,2,1);
grid->addWidget(numbt[0],3,0,1,1);
grid->addWidget(dotbt,3,1,1,2);
grid->addWidget(addbt,3,3,1,1);
QVBoxLayout *mainbox = new QVBoxLayout;
mainbox->addLayout(hbox);
mainbox->addLayout(grid);
setLayout(mainbox);
connect(delbt, SIGNAL(clicked(bool)), this, SLOT(tuiwei()));
for(int k1 = 0;k1<10;k1++){
connect(numbt[k1],SIGNAL(clicked(bool)),this,SLOT(num_pushed()));
}
connect(addbt,SIGNAL(clicked(bool)),this,SLOT(ope_pushed()));
connect(subbt,SIGNAL(clicked(bool)),this,SLOT(ope_pushed()));
connect(mulbt,SIGNAL(clicked(bool)),this,SLOT(ope_pushed()));
connect(debt,SIGNAL(clicked(bool)),this,SLOT(ope_pushed()));
connect(dotbt,SIGNAL(clicked(bool)),this,SLOT(dot()));
connect(calbt,SIGNAL(clicked(bool)),this,SLOT(cal()));
connect(clearbt,SIGNAL(clicked(bool)),this,SLOT(Clear()));
}
Widget::~Widget()
{
}