Qt实现简单计算器

环境:VS2015集成Qt5.8

计算器功能:对整数实现+、-、*、/、清空编辑框五个小功能。
先看效果图:

Qt实现简单计算器_第1张图片


先说一下思路:
第一步:创建Qt工程
第二步:创建需要的控件,并放在布局中
第三步:实现信号和槽的连接
第四步:实现槽函数
ok,其实Qt不过是一个框架,而且让你实现起来比java语言更容易。


一:创建Qt工程,工程名calculator,继承QDialog,如下图:
Qt实现简单计算器_第2张图片
为了练练手,就没有使用Ui设计师了,所以看看这个目录,只有三个主要的文件:
1:calculatordialog.h:
主要干两件事,声明需要的部件,声明信号和槽函数
2:calculatordialog.cpp:
主要干三件事:初始化部件、实现connect函数、实现槽函数
3:main.cpp://程序入口,创建我们定义的对话框


二:效果看了,思路有了,直接上代码
代码看着有点多,别怕,简单的很,因为太多都是重复的代码。

calculatordialog.h
#ifndef CALCULATORDIALOG_H
#define CALCULATORDIALOG_H

#include 
//#include "ui_calculatordialog.h"
#include 
#include 
#include  
//这里采用第二种方法加载UI文件,因为是自己的部件,所以不用Ui
class calculatorDialog : public QDialog //,public Ui::calculatorDialog
{
    Q_OBJECT
public:
    explicit calculatorDialog(QWidget *parent = 0);
    ~calculatorDialog();
protected:
    //声明需要的部件
    QPushButton* button_0;
    QPushButton* button_1;
    QPushButton* button_2;
    QPushButton* button_3;
    QPushButton* button_4;
    QPushButton* button_5;
    QPushButton* button_6;
    QPushButton* button_7;
    QPushButton* button_8;
    QPushButton* button_9;
    QPushButton* button_plus;
    QPushButton* button_minus;
    QPushButton* button_multiply;
    QPushButton* button_devide;
    QPushButton* button_equal;
    QPushButton* button_clearAll;

    QLineEdit* lineEdit;

    //声明需要的变量
    //第一个数,第二个数,运算结果
    float num1,num2,result; 
    QString S; //用于显示在lineEdit里面的字符串
    //等号运算符的时候,判断是加、减、乘、除的哪一个
    char sign;
    int mark; //若点击过加减乘除,mark=2,默认是1

    //声明处理按钮点击信号的槽函数
private slots:
    //这种完全按照UI设计师默认的命名规则,          //on_objectName_signalName
    void on_button_0_clicked();  
    void on_button_1_clicked();
    void on_button_2_clicked();
    void on_button_3_clicked();
    void on_button_4_clicked();
    void on_button_5_clicked();
    void on_button_6_clicked();
    void on_button_7_clicked();
    void on_button_8_clicked();
    void on_button_9_clicked();

    void on_button_plus_clicked();
    void on_button_minus_clicked();
    void on_button_multiply_clicked();
    void on_button_devide_clicked();
    void on_button_equal_clicked();
    void on_button_clearAll_clicked();
};
#endif // CALCULATORDIALOG_H
calculatordialog.cpp
#include "calculatordialog.h"
//#include "ui_calculatordialog.h"
#include 

calculatorDialog::calculatorDialog(QWidget *parent) :QDialog(parent)
{
    //1:初始化变量
    num1 = 0.0;   //第一个数初始化为0
    num2 = 0.0;    //第二个数初始化为0
    result = 0.0;   //resutlt初始化为0
    S="";   //显示在编辑框的字符串初始化为空
    mark=1;   //表示没有点击过加减乘除
    //初始化部件对象指针
    button_0= new QPushButton("0");
    button_1= new QPushButton("1");
    button_2= new QPushButton("2");
    button_3= new QPushButton("3");
    button_4= new QPushButton("4");
    button_5= new QPushButton("5");
    button_6= new QPushButton("6");
    button_7= new QPushButton("7");
    button_8= new QPushButton("8");
    button_9= new QPushButton("9");
    button_plus= new QPushButton("+");
    button_minus= new QPushButton("-");
    button_multiply= new QPushButton("*");
    button_devide= new QPushButton("/");
    button_equal= new QPushButton("=");
    button_clearAll= new QPushButton("c");

    lineEdit = new QLineEdit("C");

//这里我用的网格布局,感觉比水平和垂直布局整合出来的方便很多
   QGridLayout *Grid = new QGridLayout;
   Grid->addWidget(lineEdit,1,1,1,4,Qt::Alignment());
   //6个参数含义:1:部件的指针;2、3:所在行和列
   //4、5:占用几行和几列;最后一个是对齐方式

   Grid->addWidget(button_0,2,1,Qt::Alignment());
   Grid->addWidget(button_1,2,2,Qt::Alignment());
   Grid->addWidget(button_2,2,3,Qt::Alignment());
   Grid->addWidget(button_3,2,4,Qt::Alignment());

   Grid->addWidget(button_4,3,1,Qt::Alignment());
   Grid->addWidget(button_5,3,2,Qt::Alignment());
   Grid->addWidget(button_6,3,3,Qt::Alignment());
   Grid->addWidget(button_7,3,4,Qt::Alignment());

   Grid->addWidget(button_8,4,1,Qt::Alignment());
   Grid->addWidget(button_9,4,2,Qt::Alignment());
   Grid->addWidget(button_plus,4,3,Qt::Alignment());
   Grid->addWidget(button_minus,4,4,Qt::Alignment());

   Grid->addWidget(button_multiply,5,1,Qt::Alignment());
   Grid->addWidget(button_devide,5,2,Qt::Alignment());
   Grid->addWidget(button_equal,5,3,Qt::Alignment());
   Grid->addWidget(button_clearAll,5,4,Qt::Alignment());

 //把Grid布局设置给当前创建的对话框对象
   this->setLayout(Grid);

//实现连接函数
 connect(button_0,SIGNAL(clicked(bool)),this,SLOT(on_button_0_clicked()));
   connect(button_1,SIGNAL(clicked(bool)),this,SLOT(on_button_1_clicked()));
   connect(button_2,SIGNAL(clicked(bool)),this,SLOT(on_button_2_clicked()));
   connect(button_3,SIGNAL(clicked(bool)),this,SLOT(on_button_3_clicked()));
   connect(button_4,SIGNAL(clicked(bool)),this,SLOT(on_button_4_clicked()));
   connect(button_5,SIGNAL(clicked(bool)),this,SLOT(on_button_5_clicked()));
   connect(button_6,SIGNAL(clicked(bool)),this,SLOT(on_button_6_clicked()));
   connect(button_7,SIGNAL(clicked(bool)),this,SLOT(on_button_7_clicked()));
   connect(button_8,SIGNAL(clicked(bool)),this,SLOT(on_button_8_clicked()));
   connect(button_9,SIGNAL(clicked(bool)),this,SLOT(on_button_9_clicked()));

   connect(button_plus,SIGNAL(clicked(bool)),this,SLOT(on_button_plus_clicked()));
   connect(button_minus,SIGNAL(clicked(bool)),this,SLOT(on_button_minus_clicked()));
   connect(button_multiply,SIGNAL(clicked(bool)),this,SLOT(on_button_multiply_clicked()));
   connect(button_devide,SIGNAL(clicked(bool)),this,SLOT(on_button_devide_clicked()));
   connect(button_equal,SIGNAL(clicked(bool)),this,SLOT(on_button_equal_clicked()));
   connect(button_clearAll,SIGNAL(clicked(bool)),this,SLOT(on_button_clearAll_clicked()));
}

calculatorDialog::~calculatorDialog()
{
}

//实现槽函数
void calculatorDialog::on_button_0_clicked()  //这种完全按照UI设计师默认的命名规则,on_objectName_signalName
{
    S+="0";
    lineEdit->setText(S);//首先把数据设置到编辑框

    if(mark==1){ //判断是不是点击过:加、减、乘、除
     //如果没有
     num1=num1*10;  //继续添加0,相当于乘以10
    }else
        {
       num2 = num2*10; 
    }

}
void calculatorDialog::on_button_1_clicked()
{
    S+="1";
    lineEdit->setText(S);
    if(mark==1){ //判断是不是点击过:加、减、乘、除
     //如果没有
     num1=num1*10+1;  //继续添加6,相当于乘以10,再加1
    }else
        {
       num2 = num2*10+1;
    }
}
void calculatorDialog::on_button_2_clicked()
{
    S+="2";
    lineEdit->setText(S);
    if(mark==1){ //判断是不是点击过:加、减、乘、除
     //如果没有
     num1=num1*10+2;
    }else
        {
       num2 = num2*10+2;
    }
}
void calculatorDialog::on_button_3_clicked()
{
    S+="3";
    lineEdit->setText(S);
       if(mark==1){ //判断是不是点击过:加、减、乘、除
            //如果没有
            num1=num1*10+3;
           }else
               {
              num2 = num2*10+3;
           }
}
void calculatorDialog::on_button_4_clicked()
{
    S+="4";
    lineEdit->setText(S);
    if(mark==1){ //判断是不是点击过:加、减、乘、除
     //如果没有
     num1=num1*10+4;
    }else
        {
       num2 = num2*10+4;
    }
}
void calculatorDialog::on_button_5_clicked()
{
    S+="5";
    lineEdit->setText(S);
    if(mark==1){ //判断是不是点击过:加、减、乘、除
     //如果没有
     num1=num1*10+5;
    }else
        {
       num2 = num2*10+5;
    }
}
void calculatorDialog::on_button_6_clicked()
{
    S+="6";
    lineEdit->setText(S);
    if(mark==1){ //判断是不是点击过:加、减、乘、除
     //如果没有
     num1=num1*10+6;
    }else
        {
       num2 = num2*10+6;
    }
}
void calculatorDialog::on_button_7_clicked()
{
    S+="7";
    lineEdit->setText(S);
    if(mark==7){ //判断是不是点击过:加、减、乘、除
     //如果没有
     num1=num1*10+7;
    }else
        {
       num2 = num2*10+7;
    }
}
void calculatorDialog::on_button_8_clicked()
{
    S+="8";
    lineEdit->setText(S);
    if(mark==1){ //判断是不是点击过:加、减、乘、除
     //如果没有
     num1=num1*10+8;
    }else
        {
       num2 = num2*10+8;
    }
}
void calculatorDialog::on_button_9_clicked()
{
    S+="9";
    lineEdit->setText(S);
    if(mark==1){ //判断是不是点击过:加、减、乘、除
     //如果没有
     num1=num1*10+9;
    }else
        {
       num2 = num2*10+9;
    }
}
void calculatorDialog::on_button_plus_clicked()
{
    S+="+";   //把+连接到字符串S后面
     sign='+';  //设置标记为+
     mark=2;   //告诉其它数字按钮,下一个点击的数,累加到num2上
    lineEdit->setText(S);
}
void calculatorDialog::on_button_minus_clicked()
{
    S+="-";
     sign='-';
     mark=2;
    lineEdit->setText(S);
}
void calculatorDialog::on_button_multiply_clicked(){
    S += "*";
    sign = '*';
    mark = 2;
    lineEdit->setText(S);
}
void calculatorDialog::on_button_devide_clicked(){
    S += "/";
    sign = '/';
    mark = 2;
    lineEdit->setText(S);
}
void calculatorDialog::on_button_equal_clicked()
{
    S+="=";
    switch (sign) {
    case '+':
       result = num1 + num2;
        break;
    case '-':
        result = num1 - num2;
        break;
    case '*':
        result = num1 * num2;
        break;
    case '/':
        result = num1 / num2;
        break;
    default:
        break;
    }
    //S+=QString::number(result, 10);  //这个函数,result必须是int类型
    S+=QString("%1").arg(result);//float转Qstring
    //S+= result+'0'; //不能这样写,result大于等于10就会报错
    lineEdit->setText(S);
}
void calculatorDialog::on_button_clearAll_clicked(){
    S="";
    lineEdit->setText(S);
    //sign=''; //如果bu'sh不设置为,其实也没有影响
    mark=1;
    num1 = 0.0;
    num2 = 0.0;
    result = 0.0;
}

特别把这句提出来说一下: this->setLayout(Grid);
说明1:别忘了添加,这个就是把我们的布局文件添加给当前创建的对话框对象;
说明2:其实纯属的Qt设计师设计的Ui是没办法显示的,也是需要有一个parent作为依托,才能显示出来。在构造函数中会有这一句话:setupUi(this);

难免有理解不到位的地方,大家多交流学习!@_@

你可能感兴趣的:(Qt)