QT 简易计算器

QT 简易计算器_第1张图片

#include "widget.h"
#include "ui_widget.h"
#include 
#include >
#include 
#include 


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);
}


你可能感兴趣的:(QT,计算器)