QT 简易计算器

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"
#include <QScriptEngine>
#include <QMessageBox>>
#include <QIcon>
#include <QFont>


widget::widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::widget)
{
    ui->setupUi(this);

    // 中文后面不加空格,无法正常编译,加了之后乱码
    this->setWindowTitle("计算器 ");

    // 设置lineEdit右边对齐
    ui->lineEdit->setAlignment(Qt::AlignRight);

    // 设置lineEdit字体大小
    QFont font("仿宋", 10);
    ui->lineEdit->setFont(font);

    // 设置删除按钮以图片形式加载
    QIcon icon("D:\\workspace\\qt\\caculator\\back.PNG");
    ui->backButton->setIcon(icon);

    // 设置等号button背景为绿色
    ui->equalButton->setStyleSheet("background:green");
}

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


void widget::on_equalButton_clicked()
{
    try {
        // 使用script中的evaluate,需要安装这个QScriptEngine插件库
        QScriptEngine engine;
        QScriptValue res = engine.evaluate(this->expression);
        QString value = res.toString();
        ui->lineEdit->setText(value);
    } catch (...) {
        QMessageBox box;
        box.setText(this->expression + "is invalid");
    }

}


void widget::on_pointButton_clicked()
{
    this->expression += ".";
    ui->lineEdit->setText(this->expression);
}


void widget::on_zeroButton_clicked()
{
    this->expression += "0";
    ui->lineEdit->setText(this->expression);
}


void widget::on_modButton_clicked()
{
    this->expression += "%";
    ui->lineEdit->setText(this->expression);
}


void widget::on_oneButton_clicked()
{

    this->expression += "1";
    ui->lineEdit->setText(this->expression);
}


void widget::on_twoButton_clicked()
{
    this->expression += "2";
    ui->lineEdit->setText(this->expression);

}


void widget::on_threeButton_clicked()
{
    this->expression += "3";
    ui->lineEdit->setText(this->expression);

}


void widget::on_addButton_clicked()
{
    this->expression += "+";
    ui->lineEdit->setText(this->expression);

}


void widget::on_sixButton_clicked()
{
    this->expression += "6";
    ui->lineEdit->setText(this->expression);

}


void widget::on_fiveButton_clicked()
{
    this->expression += "5";
    ui->lineEdit->setText(this->expression);

}


void widget::on_fourButton_clicked()
{
    this->expression += "4";
    ui->lineEdit->setText(this->expression);
}


void widget::on_subButton_clicked()
{
    this->expression += "-";
    ui->lineEdit->setText(this->expression);
}


void widget::on_nineButton_clicked()
{
    this->expression += "9";
    ui->lineEdit->setText(this->expression);
}


void widget::on_eightButton_clicked()
{
    this->expression += "8";
    ui->lineEdit->setText(this->expression);
}


void widget::on_sevenButton_clicked()
{
    this->expression += "7";
    ui->lineEdit->setText(this->expression);
}


void widget::on_backButton_clicked()
{
    this->expression.chop(1);
    ui->lineEdit->setText(this->expression);

}


void widget::on_mutiButton_clicked()
{
    this->expression += "*";
    ui->lineEdit->setText(this->expression);
}


void widget::on_divButton_clicked()
{
    this->expression += "/";
    ui->lineEdit->setText(this->expression);
}


void widget::on_clearButton_clicked()
{
    this->expression.clear();
    ui->lineEdit->setText(this->expression);
}
相关推荐
_Kirito3 个月前
python3.9+wxPython设计的一个简单的计算器
开发语言·python·wxpython·gui·图形界面·计算器·wx
熬耶3 个月前
Uniapp之微信小程序计算器
微信小程序·小程序·uni-app·计算器
qq:9536925385 个月前
阿奇科技 简单java-swing计算器源码(可用于课设等)
java·课程设计·swing·计算器·软件/插件·calculator
mac毒7 个月前
Soulver v3.10.3.1 mac版 智能文本计算器 兼容 M1/M2/M3
macos·mac软件·计算器
SOC罗三炮8 个月前
开源,微信小程序-超级计算器T3000 简介
微信小程序·计算器·矩阵计算·解方程·math.js·分式计算·色环电阻
Miracle_86.8 个月前
灵魂指针,教给(三)
c语言·开发语言·程序人生·算法·学习方法·指针·计算器
森旺电子1 年前
51单片机四位数码管计算器 Proteus仿真程序
51单片机·proteus·计算器
Srlua1 年前
计算器——可支持小数的任意四则运算(中缀表达式转为后缀表达式算法)
c++·算法·计算器·表达式