一、使用LCD Number、Push Button和GridLayout编辑计算器页面。
二、代码
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
//使用枚举表示运算类型
enum oper
{
oper_add,
oper_sub,
oper_mul,
oper_div,
};
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_pushButton_1_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_add_clicked();
void on_pushButton_4_clicked();
void on_pushButton_5_clicked();
void on_pushButton_6_clicked();
void on_pushButton_sub_clicked();
void on_pushButton_7_clicked();
void on_pushButton_8_clicked();
void on_pushButton_9_clicked();
void on_pushButton_mul_clicked();
void on_pushButton_0_clicked();
void on_pushButton_equ_clicked();
void on_pushButton_div_clicked();
void on_pushButton_clear_clicked();
private:
Ui::Widget *ui;
//记录当前值
int num;
//上一个操作数
int temp;
//记录当前运算
oper op;
};
#endif // WIDGET_H
main.cpp文章来源:https://www.toymoban.com/news/detail-520789.html
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.cpp文章来源地址https://www.toymoban.com/news/detail-520789.html
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//初始化
num = 0;
temp = 0;
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_1_clicked()
{
num = num*10+1;
//显示
ui->lcdNumber->display(num);
}
void Widget::on_pushButton_2_clicked()
{
num = num*10+2;
//显示
ui->lcdNumber->display(num);
}
void Widget::on_pushButton_3_clicked()
{
num = num*10+3;
//显示
ui->lcdNumber->display(num);
}
void Widget::on_pushButton_4_clicked()
{
num = num*10+4;
//显示
ui->lcdNumber->display(num);
}
void Widget::on_pushButton_5_clicked()
{
num = num*10+5;
//显示
ui->lcdNumber->display(num);
}
void Widget::on_pushButton_6_clicked()
{
num = num*10+6;
//显示
ui->lcdNumber->display(num);
}
void Widget::on_pushButton_7_clicked()
{
num = num*10+7;
//显示
ui->lcdNumber->display(num);
}
void Widget::on_pushButton_8_clicked()
{
num = num*10+8;
//显示
ui->lcdNumber->display(num);
}
void Widget::on_pushButton_9_clicked()
{
num = num*10+9;
//显示
ui->lcdNumber->display(num);
}
void Widget::on_pushButton_0_clicked()
{
num = num*10;
//显示
ui->lcdNumber->display(num);
}
void Widget::on_pushButton_add_clicked()
{
if(temp>0)
{
on_pushButton_clear_clicked();
}
op = oper_add;
temp = num;
num = 0;
}
void Widget::on_pushButton_sub_clicked()
{
if(temp>0)
{
on_pushButton_clear_clicked();
}
op = oper_sub;
temp = num;
num = 0;
}
void Widget::on_pushButton_mul_clicked()
{
if(temp>0)
{
on_pushButton_clear_clicked();
}
op = oper_mul;
temp = num;
num = 0;
}
void Widget::on_pushButton_div_clicked()
{
if(temp>0)
{
on_pushButton_clear_clicked();
}
op = oper_div;
temp = num;
num = 0;
}
void Widget::on_pushButton_equ_clicked()
{
switch (op) {
case oper_add:
num = temp+num;
break;
case oper_sub:
num = temp-num;
break;
case oper_mul:
num = temp*num;
break;
case oper_div:
if(num == 0)
{
num = 0;
temp = 0;
ui->lcdNumber->display("Err");
return;
}
num = temp/num;
break;
}
temp = 0;
ui->lcdNumber->display(num);
}
void Widget::on_pushButton_clear_clicked()
{
num = 0;
temp = 0;
ui->lcdNumber->display(num);
}
到了这里,关于Qt编写简单计算器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!