用QT实现一个系统计算器 图文并茂,详细的教学方式

13 篇文章 1 订阅
5 篇文章 3 订阅
	先看动态图,该计算器实现了按钮点击或者是手写的方式计算出结果,其中用户所输入的数字也会在输出结果框上进行实时显示,基本满足了计算器所拥有的的功能。完整的代码将会在文章下方给出,免费下载。
下载链接:https://download.csdn.net/download/m0_46217142/21480935
我设置的是0积分下载,应该可以免费下载,话不多说,看效果图

请添加图片描述
下面进行代码讲解: 如果你是新手,可以跟着从头开始建项目,首先你必须要先下载好QT。跟着视频来先创建一个项目。请添加图片描述
创建好项目后在widget.app下编写程序

在这里插入图片描述

 首先包含头文件。这是该计算器包含的头文件,其实只用到了”widget.h“,"QLineEdit","QPushButton",
 分别是窗体的头文件,编辑框的头文件和按键的头文件。
#include "widget.h"
#include "ui_widget.h"
#include <QtDebug>
#include <QLineEdit>
#include <QPushButton>

接下来创建好界面,如按钮,编辑框等东西,并对界面进行位置的布局。

 this->setWindowTitle("计算器");
    QLineEdit  *plineedit=new QLineEdit(this);
    plineedit->resize(260,50);
    plineedit->move(20,20);
    plineedit->setPlaceholderText("输出结果框");
    plineedit->setFont(QFont( "Timers" , 10 ,  QFont::Bold) );
    plineedit->setReadOnly(true);
    //加减乘除
    QPushButton *bt0=new QPushButton("+",this);
    QPushButton *bt1=new QPushButton("-",this);
    QPushButton *bt2=new QPushButton("*",this);
    QPushButton *bt3=new QPushButton("/",this);
    QPushButton *bt4=new QPushButton("=",this);
    bt0->resize(100,50);
    bt1->resize(100,50);
    bt2->resize(100,50);
    bt3->resize(100,50);
    bt4->resize(100,50);
    bt0->move(200,100);
    bt1->move(200,170);
    bt3->move(200,240);
    bt2->move(200,310);
    bt4->move(200,380);
    //数字 0-9
    QPushButton *num0=new QPushButton("0",this);
    QPushButton *num1=new QPushButton("1",this);
    QPushButton *num2=new QPushButton("2",this);
    QPushButton *num3=new QPushButton("3",this);
    QPushButton *num4=new QPushButton("4",this);
    QPushButton *num5=new QPushButton("5",this);
    QPushButton *num6=new QPushButton("6",this);
    QPushButton *num7=new QPushButton("7",this);
    QPushButton *num8=new QPushButton("8",this);
    QPushButton *num9=new QPushButton("9",this);
    QPushButton *bt_clear=new QPushButton("C",this);
    num0->resize(50,50);
    num0->move(0,100);
    num1->resize(50,50);
    num1->move(70,100);
    num2->resize(50,50);
    num2->move(140,100);
    num3->resize(50,50);
    num3->move(0,170);
    num4->resize(50,50);
    num4->move(70,170);
    num5->resize(50,50);
    num5->move(140,170);
    num6->resize(50,50);
    num6->move(0,240);
    num7->resize(50,50);
    num7->move(70,240);
    num8->resize(50,50);
    num8->move(140,240);
    num9->resize(50,50);
    num9->move(0,310);
    bt_clear->resize(50,50);
    bt_clear->move(0,380);
    //自己写的框
    QLineEdit  *lab0= new QLineEdit(this);
    QLineEdit *lab1= new QLineEdit(this);
    lab0->resize(130,50);
    lab0->move(60,310);
    lab1->resize(130,50);
    lab1->move(60,380);
    lab0->setPlaceholderText("请输入第一个数字");
    lab1->setPlaceholderText("请输入第二个数字");
    lab1->setClearButtonEnabled(true);
    lab0->setClearButtonEnabled(true);
这样界面就创建好了,这上面只用看几个函数,如果不懂得可以去百度以下。
  • 接下来开始让各个按键进行通信,使用connec函数进行数据的转发,在这里简单解释以下这个函数的用法。
  • 格式:connect(发送,并发送:: signal,receiver ,; className :: ::,信号接收者对象(指针),classB :: slot);收货人::插槽),一共四个参数我槽函数使用的是lambda表达式:比如这个 connect(num0,&QPushButton::clicked,this,={
    numsum.append(“0”);
    plineedit->setText(numsum);
    });
tatic QString numsum="";
    connect(num0,&QPushButton::clicked,this,[=](){
        numsum.append("0");
        plineedit->setText(numsum);
    });
    connect(num1,&QPushButton::clicked,this,[=](){
        numsum.append("1");
        plineedit->setText(numsum);
    });
    connect(num2,&QPushButton::clicked,this,[=](){
        numsum.append("2");
        plineedit->setText(numsum);
    });
    connect(num3,&QPushButton::clicked,this,[=](){
        numsum.append("3");
        plineedit->setText(numsum);
    });
    connect(num4,&QPushButton::clicked,this,[=](){
        numsum.append("4");
        plineedit->setText(numsum);
    });
    connect(num5,&QPushButton::clicked,this,[=](){
        numsum.append("5");
        plineedit->setText(numsum);
    });
    connect(num6,&QPushButton::clicked,this,[=](){
        numsum.append("6");
        plineedit->setText(numsum);
    });
    connect(num7,&QPushButton::clicked,this,[=](){
        numsum.append("7");
        plineedit->setText(numsum);
    });
    connect(num8,&QPushButton::clicked,this,[=](){
        numsum.append("8");
        plineedit->setText(numsum);
    });
    connect(num9,&QPushButton::clicked,this,[=](){
        numsum.append("9");
        plineedit->setText(numsum);
    });
    //运算


    connect(bt0,&QPushButton::clicked,this,[=](){fuhao='+';all1=numsum;numsum="";});
    connect(bt1,&QPushButton::clicked,this,[=](){fuhao='-';all1=numsum;numsum="";});
    connect(bt2,&QPushButton::clicked,this,[=](){fuhao='*';all1=numsum;numsum="";});
    connect(bt3,&QPushButton::clicked,this,[=](){fuhao='/';all1=numsum;numsum="";});
    connect(bt_clear,&QPushButton::clicked,this,[=](){
        lab0->setText("");
        lab1->setText("");
        fuhao=' ';
        plineedit->setText("");});
    connect(lab0,&QLineEdit::textEdited,this,[=](){
        myqstr1=lab0->text()+fuhao+lab1->text();
        plineedit->setText(myqstr1);});
    connect(lab1,&QLineEdit::textEdited,this,[=](){
        myqstr1=lab0->text()+fuhao+lab1->text();
        plineedit->setText(myqstr1);});
    connect(bt4,&QPushButton::clicked,this,[=](){
        QString midstr1=lab0->text();//就是返回行编辑器里面的内容
        QString midstr2=lab1->text();
        str1=midstr1.toStdString();
        str2=midstr2.toStdString();
        char *c1=(char*)str1.c_str();
        char *c2=(char*)str2.c_str();
        //新的一种转换方式
        char *c4=NULL;
        string h1,h2;
        h1=all1.toStdString();
        qDebug()<<all1;
        c4=(char*)h1.c_str();
        h2=numsum.toStdString();
        char *c3=(char*)h2.c_str();
        midnum=atof(c4)+atof(c3);
        if(all1!=" ")
        {
            qDebug()<<1;
            c1=c4;
        }
        if(numsum!="")
        {
            qDebug()<<2;
            c2=c3;
        }
        qDebug()<<midnum;
        numsum=" ";
        //判断是哪一个符号
        switch (fuhao) {
        case '+':midvar=atof(c1)+atof(c2);
            break;
        case '-':midvar=atof(c1)-atof(c2);
            break;
        case '*':midvar=atof(c1)*atof(c2);
            break;
        case '/':midvar=atof(c1)/atof(c2);
            break;

        }
        sprintf(istr,"%.2f",midvar);
        string strstr=istr;
        QString myqstr=QString::fromStdString(strstr);

        plineedit->setText(myqstr);
    });
}

这上面包含了各种数据类型的转换,如果不懂得这些函数得意思可以一一去百度以下。

  • 8
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 18
    评论
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魔动山霸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值