• 【QT】简易计算器实现


    主要为了配合学习QT,还有一些bug,先不深究

    #include "calc.h"
    #include "ui_calc.h"
    
    calc::calc(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::calc)
    {
        ui->setupUi(this);
    
        QFont font=this->font();
        font.setPixelSize(40);
        this->setFont(font);
        window_init();
        key_init();
    }
    
    calc::~calc()
    {
        delete ui;
    }
    
    void calc::window_init()
    {
        this->setMinimumSize(350,400);
        this->setMaximumSize(350,400);
        this->setWindowTitle("计算器");
    
        vbox=new QVBoxLayout(ui->centralWidget);
        title=new QLabel("计算器");
        title->setAlignment(Qt::AlignHCenter);
    
        vbox->addWidget(title);
    
        line=new QLineEdit();
        line->setMinimumHeight(60);
        line->setMaximumHeight(60);
        line->setAlignment(Qt::AlignRight);
    
        vbox->addWidget(line);
    }
    
    void calc::key_init()
    {
        QString str="789+456-123*.0=/";
        grid =new QGridLayout();
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                button[i*4+j]=new QPushButton();
                button[i*4+j]->setText(str.at(i*4+j));
                button[i*4+j]->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    //            connect(button[i*4+j],SIGNAL(clicked()),this,SLOT(button_click()));
                connect(button[i*4+j],&QPushButton::clicked,this,&calc::button_click);
                grid->addWidget(button[i*4+j],i,j);
            }
        }
    
        vbox->addLayout(grid);
    }
    
    void calc::button_click()
    {
    
        QPushButton *button=static_cast<QPushButton *>(sender());
        QString ch=button->text();
        if(QString("0123456789.").contains(ch)>0)
        {
            inputnum(ch);
        }   
        else if(QString("+-*/").contains(ch)>0)
        {
            inputtype(ch);
            qDebug()<<ch+"inputtype";
        }
        else //=
        {
            output();
        }
    }
    
    void calc::inputnum(QString ch)
    {
        if(clacs == 0)
        {
            Anum.append(ch);
            line->setText(Anum);
        }
        else{
            Bnum.append(ch);
            line->setText(Bnum);
    
            switch(clacs)
            {
                  case 1:qDebug()<<clacs;line->setText(Anum+"+"+Bnum);break;
                  case 2:qDebug()<<clacs;line->setText(Anum+"-"+Bnum);break;
                  case 3:qDebug()<<clacs;line->setText(Anum+"*"+Bnum);break;
                  case 4:qDebug()<<clacs;line->setText(Anum+"/"+Bnum);break;
    
            }
        }
    }
    
    void calc::inputtype(QString ch)
    {
        switch(ch.at(0).toLatin1())
        {
    //        case '+':clacs=1;break;
    //        case '-':clacs=2;break;
    //        case '*':clacs=3;break;
    //        case '/':clacs=4;break;
              case '+':clacs=1;line->setText(Anum+"+");break;
              case '-':clacs=2;line->setText(Anum+"-");break;
              case '*':clacs=3;line->setText(Anum+"*");break;
              case '/':clacs=4;line->setText(Anum+"/");break;
        default:break;
    
        }
    }
    
    void calc::output()
    {
        QString sum;
        switch (clacs) {
        case 1:sum=Anum+"+"+Bnum+"="+QString::number(Anum.toDouble()+Bnum.toDouble());break;
        case 2:sum=Anum+"-"+Bnum+"="+QString::number(Anum.toDouble()-Bnum.toDouble());break;
        case 3:sum=Anum+"*"+Bnum+"="+QString::number(Anum.toDouble()*Bnum.toDouble());break;
        case 4:sum=Anum+"/"+Bnum+"="+QString::number(Anum.toDouble()/Bnum.toDouble());break;
        default:
            break;
        }
        line->setText(sum);
    
        clacs=0;
        Anum.clear();
        Bnum.clear();
    }

    头文件.h代码

    #ifndef CALC_H
    #define CALC_H
    
    #include <QMainWindow>
    #include <QGridLayout>
    #include <QLabel>
    #include <QLineEdit>
    #include <QPushButton>
    #include <QVBoxLayout>
    #include <QDebug>
    #include <QString>
    
    namespace Ui {
    class calc;
    }
    
    class calc : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit calc(QWidget *parent = 0);
        ~calc();
        void window_init();
        void key_init();
        void inputnum(QString ch);
        void inputtype(QString ch);
        void output();
    private slots:
        void button_click();
    private:
        Ui::calc *ui;
        QLabel *title;
        QLineEdit *line;
        QPushButton *button[16];
        QGridLayout *grid;
        QVBoxLayout *vbox;
    
        QString Anum;
        QString Bnum;
        int clacs=0;
    };
    
    #endif // CALC_H

    下面,对代码做简单解释

    文本框与标题放在一个函数初始化,键盘放在另一个函数进行初始化

    void calc::window_init()
    {
        this->setMinimumSize(350,400);
        this->setMaximumSize(350,400);
        this->setWindowTitle("计算器");
    
        vbox=new QVBoxLayout(ui->centralWidget);
        title=new QLabel("计算器");
        title->setAlignment(Qt::AlignHCenter);
    
        vbox->addWidget(title);
    
        line=new QLineEdit();
        line->setMinimumHeight(60);
        line->setMaximumHeight(60);
        line->setAlignment(Qt::AlignRight);
    
        vbox->addWidget(line);
    }

    对键盘进行初始化,并对点击按钮建立槽函数

    void calc::key_init()
    {
        QString str="789+456-123*.0=/";
        grid =new QGridLayout();
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                button[i*4+j]=new QPushButton();
                button[i*4+j]->setText(str.at(i*4+j));
                button[i*4+j]->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    //            connect(button[i*4+j],SIGNAL(clicked()),this,SLOT(button_click()));
                connect(button[i*4+j],&QPushButton::clicked,this,&calc::button_click);
                grid->addWidget(button[i*4+j],i,j);
            }
        }
    
        vbox->addLayout(grid);
    }

    点击按钮的槽函数实现,如果是数字实现inputnum(ch);,如果是加减乘除实现inputtype(ch);,如果是等于实现output();

    void calc::button_click()
    {
    
        QPushButton *button=static_cast<QPushButton *>(sender());
        QString ch=button->text();
        if(QString("0123456789.").contains(ch)>0)
        {
            inputnum(ch);
        }   
        else if(QString("+-*/").contains(ch)>0)
        {
            inputtype(ch);
            qDebug()<<ch+"inputtype";
        }
        else //=
        {
            output();
        }
    }

    第一个数字存在Anum中,第二个数字存在Bnum中

    void calc::inputnum(QString ch)
    {
        if(clacs == 0)
        {
            Anum.append(ch);
            line->setText(Anum);
        }
        else{
            Bnum.append(ch);
            line->setText(Bnum);
    
            switch(clacs)
            {
                  case 1:qDebug()<<clacs;line->setText(Anum+"+"+Bnum);break;
                  case 2:qDebug()<<clacs;line->setText(Anum+"-"+Bnum);break;
                  case 3:qDebug()<<clacs;line->setText(Anum+"*"+Bnum);break;
                  case 4:qDebug()<<clacs;line->setText(Anum+"/"+Bnum);break;
    
            }
        }
    }

     判断加减乘除操作,对clacs变量进行相应的赋值

    void calc::inputtype(QString ch)
    {
        switch(ch.at(0).toLatin1())
        {
    //        case '+':clacs=1;break;
    //        case '-':clacs=2;break;
    //        case '*':clacs=3;break;
    //        case '/':clacs=4;break;
              case '+':clacs=1;line->setText(Anum+"+");break;
              case '-':clacs=2;line->setText(Anum+"-");break;
              case '*':clacs=3;line->setText(Anum+"*");break;
              case '/':clacs=4;line->setText(Anum+"/");break;
        default:break;
    
        }
    }

    输出结果,讲clacs值重置为0

    void calc::output()
    {
        QString sum;
        switch (clacs) {
        case 1:sum=Anum+"+"+Bnum+"="+QString::number(Anum.toDouble()+Bnum.toDouble());break;
        case 2:sum=Anum+"-"+Bnum+"="+QString::number(Anum.toDouble()-Bnum.toDouble());break;
        case 3:sum=Anum+"*"+Bnum+"="+QString::number(Anum.toDouble()*Bnum.toDouble());break;
        case 4:sum=Anum+"/"+Bnum+"="+QString::number(Anum.toDouble()/Bnum.toDouble());break;
        default:
            break;
        }
        line->setText(sum);
    
        clacs=0;
        Anum.clear();
        Bnum.clear();
    }
  • 相关阅读:
    RESTful API 介绍,设计
    golang web框架设计7:整合框架
    golang web框架设计6:上下文设计
    golang web框架设计5:配置设计
    golang web框架设计4:日志设计
    golang web框架设计3:controller设计
    golang web框架设计2:自定义路由
    golang web框架设计1:框架规划
    深入理解golang: channels
    服务端高并发分布式十四次架构演进之路
  • 原文地址:https://www.cnblogs.com/powercool/p/14458597.html
Copyright © 2020-2023  润新知