QT专题1:实现一个简易计算器

QT程序开发流程如下:

1.申明必要的部件。

2.将部件构造出来

3必要的布局

4.前后台进行关联

5.美化(QSS)

QT基础部件:

1.按钮类:普通按钮,工具按钮,单选按钮,多选按钮,命令连接按钮

2.布局类:水平布局,垂直布局,网格布局

3.输出类:标签,文本浏览器,日历,七段数码管,进度条

4.输入类

5.容器

下面通过用QT实现一个简易计算器来初步了解QT的使用:

首先我们要先考虑计算器的需要使用到那些部件,0~9的数字按钮键,还有加减乘除运算符的按键,以及清除键,和一个文本框来显示我们输入以及计算的结果。

那么首先我们需要在头文件中去声明这些按钮!!这里我们用到了两个类,分别是QLineEdit和QPushButton

    //1. 申明出未来需要的控件
    QLineEdit *le_lcd;//显示框
    QPushButton *bt_num[10];//数字键
    QPushButton *bt_add;//+号键
    QPushButton *bt_sub;//-
    QPushButton *bt_mul;//*
    QPushButton *bt_div;// /
    QPushButton *bt_calc;// =
    QPushButton *bt_chop;//清除

在我们输入两个数以及运算符后,我们需要将两个数字和运算符进行保存,这样方便我们进行计算所以我们还需要类头文件中定义三个 变量来分别保存我们的数字和运算符

    char op;//保存运算符
    int data1;//保存计算数1
    int data2;//保存计算数2

随后我们需要去我们的cpp文件中,将这些部件构造出来!

 le_lcd = new QLineEdit;
 bt_chop = new QPushButton("<");

    for(int i=0; i<10; i++)
        bt_num[i] = new QPushButton(QString::number(i));

    bt_add = new QPushButton("+");
    bt_sub = new QPushButton("-");
    bt_mul = new QPushButton("*");
    bt_div = new QPushButton("/");
    bt_calc = new QPushButton("=");

随后我们要对这些按钮进行布局排版,还记得我们的的三种布局吗,水平布局QHBoxLayout,垂直布局QVBoxLayout,网格布局 QGridLayout,当然这几种布局我们可以混合使用!!

 //3. 布局
    QHBoxLayout *hbox = new QHBoxLayout;
    hbox->addWidget(le_lcd);
    hbox->addWidget(bt_chop);
    QGridLayout *gbox = new QGridLayout;
    int i = 0;
    for(int y=0; y<3; y++)
        for(int x=0; x<3; x++)
            gbox->addWidget(bt_num[i++], y, x);
    gbox->addWidget(bt_num[9], 3, 0);
    gbox->addWidget(bt_add, 0, 3);
    gbox->addWidget(bt_sub, 1, 3);
    gbox->addWidget(bt_mul, 2, 3);
    gbox->addWidget(bt_div, 3, 1);
    gbox->addWidget(bt_calc, 3, 2, 1, 2);
    QVBoxLayout *mainbox = new QVBoxLayout;
    mainbox->addLayout(hbox);
    mainbox->addLayout(gbox);
    setLayout(mainbox);

布局过后,我们需要将我们的按钮和功能相关联起来,这时候我们需要使用槽函数来进行前后台的关联,那么我们先在头文件中声明我们要创建的槽函数!

public slots:
    void num_pressed(void); //按键槽函数
    void del_num(void); //删除一个输入
    void get_op(void);  //运算符按键槽
    void calculate(); //计算槽

将按钮和槽函数进行关联:

  //4. 前后台关联
    for(int i=0; i<10; i++)
        connect(bt_num[i], SIGNAL(clicked(bool)), this, SLOT(num_pressed()));
    connect(bt_chop, SIGNAL(clicked(bool)), this, SLOT(del_num()));
    connect(bt_add, SIGNAL(clicked(bool)), this, SLOT(get_op()));
    connect(bt_sub, SIGNAL(clicked(bool)), this, SLOT(get_op()));
    connect(bt_mul, SIGNAL(clicked(bool)), this, SLOT(get_op()));
    connect(bt_div, SIGNAL(clicked(bool)), this, SLOT(get_op()));
    connect(bt_calc, SIGNAL(clicked(bool)), this, SLOT(calculate()));

随后我们在cpp文件中对槽函数的功能进行编写

void Widget::del_num()
{
    QString str = le_lcd->text();
    str.chop(1);
    le_lcd->setText(str);
}

void Widget::get_op()
{
    //1. 提取按键
    QPushButton *xbt = static_cast<QPushButton*>( sender() );
    //2. 保存符号
    op = xbt->text().toStdString().c_str()[0];
    //3. 提取第一个运算数
    data1 = le_lcd->text().toInt();
    le_lcd->clear();
}

void Widget::calculate()
{
    //0. 提取data2
    data2 = le_lcd->text().toInt();
    //1. 计算
    int answer;
    switch (op) {
    case '+':
        answer = data1 + data2;
        break;
    case '-':
        answer = data1 - data2;
        break;
    case '*':
        answer = data1 * data2;
        break;
    case '/':
        answer = data1 / data2;
        break;
    default:
        break;
    }
    //2. 显示
    le_lcd->setText(QString::number(answer));
}

void Widget::num_pressed(void)
{
    //1. 提取按键
    QPushButton *xbt = static_cast<QPushButton*>( sender() );
    //2. 显示数据
    le_lcd->setText(le_lcd->text().append(xbt->text()));
}

那么这样一个简易的计算器就完成了!!!

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值