• QT 简易计算器


    在这里插入图片描述

    #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);
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
  • 相关阅读:
    Ansible 的脚本 --- playbook 剧本
    业务中我们如何更新缓存?Redis
    日常踩坑-[sass]Error: Expected newline
    Git管理远程仓库以及 git push 中遇到的问题
    基于php开发的外卖订餐网站
    基于可视图法(VG)的路径规划算法简述
    Spring Bean的作用域有哪些?它的注册方式有几种?
    Java引用和内部类
    Virtio Over MMIO
    HttpServletResponse 类
  • 原文地址:https://blog.csdn.net/CodeHouse/article/details/132748343