基于Qt下gui的简易计算器设计

基于Qt下gui的简易计算器设计
做的这个简易计算器,简易是因为只实现了简单的一次加减乘除
直接上代码吧:

mainwindow.h

//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    
private slots:
    void on_pushButton_07_clicked();

    void on_pushButton_08_clicked();

    void on_pushButton_09_clicked();

    void on_pushButton_add_clicked();

    void on_pushButton_04_clicked();

    void on_pushButton_05_clicked();

    void on_pushButton_06_clicked();

    void on_pushButton_sub_clicked();

    void on_pushButton_01_clicked();

    void on_pushButton_02_clicked();

    void on_pushButton_03_clicked();

    void on_pushButton_mul_clicked();

    void on_pushButton_0_clicked();

    void on_pushButton_dian_clicked();

    void on_pushButton_ac_clicked();

    void on_pushButton_div_clicked();

    void on_pushButton_del_clicked();

    void on_pushButton_eq_clicked();
    
private:
    Ui::MainWindow *ui;
    bool dotflag;
    char operation;
    bool cleanflag;
    double num1,num2,res;
};

#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);
    dotflag=false;
    operation=0;
    cleanflag=false;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_07_clicked()
{
    if(operation!=0 && cleanflag==true)
    {
        ui->lineEdit->clear();
        dotflag=false;
        cleanflag=false;
    }
    QString str = ui ->lineEdit->text();
    if(str == ""|| str =="0")
    {
        ui ->lineEdit->setText("7");

    }else {
        str= str +"7";
        ui ->lineEdit->setText(str);
    }
}

void MainWindow::on_pushButton_08_clicked()
{
    if(operation!=0 && cleanflag==true)
    {
        ui->lineEdit->clear();
        dotflag=false;
        cleanflag=false;
    }
    QString str = ui ->lineEdit->text();
    if(str == ""|| str =="0")
    {
        ui ->lineEdit->setText("8");

    }else {
        str= str +"8";
        ui ->lineEdit->setText(str);
    }
}

void MainWindow::on_pushButton_09_clicked()
{
    if(operation!=0 && cleanflag==true)
    {
        ui->lineEdit->clear();
        dotflag=false;
        cleanflag=false;
    }
    QString str = ui ->lineEdit->text();
    if(str == ""|| str =="0")
    {
        ui ->lineEdit->setText("9");

    }else {
        str= str +"9";
        ui ->lineEdit->setText(str);
    }
}

void MainWindow::on_pushButton_add_clicked()
{
    switch(operation){
        case 1:
            num2=ui->lineEdit->text().toDouble();
            res=num1+num2;
            ui->lineEdit->setText(QString::number(res));
            break;
        case 2:
            num2=ui->lineEdit->text().toDouble();
            res=num1-num2;
            ui->lineEdit->setText(QString::number(res));
            break;
        case 3:
            num2=ui->lineEdit->text().toDouble();
            res=num1*num2;
            ui->lineEdit->setText(QString::number(res));
            break;
        case 4:
            num2=ui->lineEdit->text().toDouble();
            res=num1/num2;
            ui->lineEdit->setText(QString::number(res));
            break;
        default:
            break;
    }
    operation=1;
    cleanflag=true;
    num1=ui->lineEdit->text().toDouble();
}

void MainWindow::on_pushButton_04_clicked()
{
    if(operation!=0 && cleanflag==true)
    {
        ui->lineEdit->clear();
        dotflag=false;
        cleanflag=false;
    }
    QString str = ui ->lineEdit->text();
    if(str == ""|| str =="0")
    {
        ui ->lineEdit->setText("4");

    }else {
        str= str +"4";
        ui ->lineEdit->setText(str);
    }
}

void MainWindow::on_pushButton_05_clicked()
{
    if(operation!=0 && cleanflag==true)
    {
        ui->lineEdit->clear();
        dotflag=false;
        cleanflag=false;
    }
    QString str = ui ->lineEdit->text();
    if(str == ""|| str =="0")
    {
        ui ->lineEdit->setText("5");

    }else {
        str= str +"5";
        ui ->lineEdit->setText(str);
    }
}

void MainWindow::on_pushButton_06_clicked()
{
    if(operation!=0 && cleanflag==true)
    {
        ui->lineEdit->clear();
        dotflag=false;
        cleanflag=false;
    }
    QString str = ui ->lineEdit->text();
    if(str == ""|| str =="0")
    {
        ui ->lineEdit->setText("6");

    }else {
        str= str +"6";
        ui ->lineEdit->setText(str);
    }
}

void MainWindow::on_pushButton_sub_clicked()
{
    switch(operation){
        case 1:
            num2=ui->lineEdit->text().toDouble();
            res=num1+num2;
            ui->lineEdit->setText(QString::number(res));
            break;
        case 2:
            num2=ui->lineEdit->text().toDouble();
            res=num1-num2;
            ui->lineEdit->setText(QString::number(res));
            break;
        case 3:
            num2=ui->lineEdit->text().toDouble();
            res=num1*num2;
            ui->lineEdit->setText(QString::number(res));
            break;
        case 4:
            num2=ui->lineEdit->text().toDouble();
            res=num1/num2;
            ui->lineEdit->setText(QString::number(res));
            break;
        default:
            break;
    }
    operation=2;
    cleanflag=true;
    num1=ui->lineEdit->text().toDouble();
}

void MainWindow::on_pushButton_01_clicked()
{
    if(operation!=0 && cleanflag==true)
    {
        ui->lineEdit->clear();
        dotflag=false;
        cleanflag=false;
    }
    QString str = ui ->lineEdit->text();
    if(str == ""|| str =="0")
    {
        ui ->lineEdit->setText("1");

    }else {
        str= str +"1";
        ui ->lineEdit->setText(str);
    }
}

void MainWindow::on_pushButton_02_clicked()
{
    if(operation!=0 && cleanflag==true)
    {
        ui->lineEdit->clear();
        dotflag=false;
        cleanflag=false;
    }
    QString str = ui ->lineEdit->text();
    if(str == ""|| str =="0")
    {
        ui ->lineEdit->setText("2");

    }else {
        str= str +"2";
        ui ->lineEdit->setText(str);
    }
}

void MainWindow::on_pushButton_03_clicked()
{
    if(operation!=0 && cleanflag==true)
    {
        ui->lineEdit->clear();
        dotflag=false;
        cleanflag=false;
    }
    QString str = ui ->lineEdit->text();
    if(str == ""|| str =="0")
    {
        ui ->lineEdit->setText("3");

    }else {
        str= str +"3";
        ui ->lineEdit->setText(str);
    }
}

void MainWindow::on_pushButton_mul_clicked()
{
    switch(operation){
        case 1:
            num2=ui->lineEdit->text().toDouble();
            res=num1+num2;
            ui->lineEdit->setText(QString::number(res));
            break;
        case 2:
            num2=ui->lineEdit->text().toDouble();
            res=num1-num2;
            ui->lineEdit->setText(QString::number(res));
            break;
        case 3:
            num2=ui->lineEdit->text().toDouble();
            res=num1*num2;
            ui->lineEdit->setText(QString::number(res));
            break;
        case 4:
            num2=ui->lineEdit->text().toDouble();
            res=num1/num2;
            ui->lineEdit->setText(QString::number(res));
            break;
        default:
            break;
    }
    operation=3;
    cleanflag=true;
    num1=ui->lineEdit->text().toDouble();
}

void MainWindow::on_pushButton_0_clicked()
{
    if(operation!=0 && cleanflag==true)
    {
        ui->lineEdit->clear();
        dotflag=false;
        cleanflag=false;
    }
    QString str = ui ->lineEdit->text();
    if(str == ""|| str =="0")
    {
        ui ->lineEdit->setText("0");

    }else {
        str= str +"0";
        ui ->lineEdit->setText(str);
    }
}

void MainWindow::on_pushButton_dian_clicked()
{
    if(operation!=0 && cleanflag==true)
    {
        ui->lineEdit->clear();
        dotflag=false;
        cleanflag=false;
    }
    QString str = ui->lineEdit->text();
    if(dotflag==false)
    {
        if(str=="")
        {
            str=str+"0.";
            ui->lineEdit->setText(str);
            dotflag=true;
        }else{
            str=str+".";
            ui->lineEdit->setText(str);
            dotflag=true;
        }
    }
}

void MainWindow::on_pushButton_ac_clicked()
{
    ui->lineEdit->clear();
    dotflag=false;
    operation=0;
    cleanflag=false;
}

void MainWindow::on_pushButton_div_clicked()
{
    switch(operation){
        case 1:
            num2=ui->lineEdit->text().toDouble();
            res=num1+num2;
            ui->lineEdit->setText(QString::number(res));
            break;
        case 2:
            num2=ui->lineEdit->text().toDouble();
            res=num1-num2;
            ui->lineEdit->setText(QString::number(res));
            break;
        case 3:
            num2=ui->lineEdit->text().toDouble();
            res=num1*num2;
            ui->lineEdit->setText(QString::number(res));
            break;
        case 4:
            num2=ui->lineEdit->text().toDouble();
            res=num1/num2;
            ui->lineEdit->setText(QString::number(res));
            break;
        default:
            break;
    }
    operation=4;
    cleanflag=true;
    num1=ui->lineEdit->text().toDouble();
}

void MainWindow::on_pushButton_del_clicked()
{
    QString str=ui->lineEdit->text();
    str=str.left(str.length()-1);
    ui->lineEdit->setText(str);
    QString tmpstr;
    tmpstr=str.right(1);
    if(tmpstr==".")
    {
        dotflag=false;
    }
    ui->lineEdit->setText(str);
}

void MainWindow::on_pushButton_eq_clicked()
{
    num2=ui->lineEdit->text().toDouble();
    switch(operation){
        case 1:
            res=num1+num2;
            ui->lineEdit->setText(QString::number(res));
            break;
        case 2:
            res=num1-num2;
            ui->lineEdit->setText(QString::number(res));
            break;
        case 3:
            res=num1*num2;
            ui->lineEdit->setText(QString::number(res));
            break;
        case 4:
            res=num1/num2;
            ui->lineEdit->setText(QString::number(res));
            break;
        default:
            break;
        operation=0;
    }
}

上一篇:java-计算器模板及源码


下一篇:从另一个线程更新GUI制作容易