文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

QT实现简单计算器功能

2024-04-02 19:55

关注

本文实例为大家分享了QT实现简单计算器功能的具体代码,供大家参考,具体内容如下

效果图:

新建工程,创建类MainWindow,基类是QMainWindow,声明变量和函数、槽

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
#include <QPushButton>
#include <QLineEdit>
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
protected:
    int num1,num2,result; //标记第一个数,第二个数和结果
    int operat; //运算符号
    int operat_flag;//标记是否输入符号
    QLineEdit *data;
    QPushButton *button_0;
    QPushButton *button_1;
    QPushButton *button_2;
    QPushButton *button_3;
    QPushButton *button_4;
    QPushButton *button_5;
    QPushButton *button_6;
    QPushButton *button_7;
    QPushButton *button_8;
    QPushButton *button_9;
    QPushButton *button_ce;
    QPushButton *button_jia;
    QPushButton *button_jian;
    QPushButton *button_cheng;
    QPushButton *button_chu;
    QPushButton *button_result;
    QString s;
 
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
 
public:
    void myLayout();
    void operation();
    void readNum(int num);
    void init_data();
private slots:
    void button_0_clicked();
    void button_1_clicked();
    void button_2_clicked();
    void button_3_clicked();
    void button_4_clicked();
    void button_5_clicked();
    void button_6_clicked();
    void button_7_clicked();
    void button_8_clicked();
    void button_9_clicked();
    void button_ce_clicked();
    void button_result_clicked();
    void button_cheng_clicked();
    void button_chu_clicked();
    void button_jia_clicked();
    void button_jian_clicked();
};
 
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include <QPushButton>
#include <QLineEdit>
#include <QGridLayout>
 
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
}
 
void MainWindow::myLayout(){
    QWidget *centerWindow = new QWidget;
    this->setCentralWidget(centerWindow);
 
    s = "";
    init_data();
    data = new QLineEdit("");
 
    button_0 = new QPushButton("0");
    button_1 = new QPushButton("1");
    button_2 = new QPushButton("2");
    button_3 = new QPushButton("3");
    button_4 = new QPushButton("4");
    button_5 = new QPushButton("5");
    button_6 = new QPushButton("6");
    button_7 = new QPushButton("7");
    button_8 = new QPushButton("8");
    button_9 = new QPushButton("9");
    button_ce = new QPushButton("CE");
    button_jia = new QPushButton("+");
    button_jian = new QPushButton("-");
    button_cheng = new QPushButton("*");
    button_chu = new QPushButton("/");
    button_result = new QPushButton("=");
 
    data->setFixedHeight(30);
    button_0->setMinimumHeight(50);
    button_1->setMinimumHeight(50);
    button_2->setMinimumHeight(50);
    button_3->setMinimumHeight(50);
    button_4->setMinimumHeight(50);
    button_5->setMinimumHeight(50);
    button_6->setMinimumHeight(50);
    button_7->setMinimumHeight(50);
    button_8->setMinimumHeight(50);
    button_9->setMinimumHeight(50);
    button_ce->setMinimumHeight(50);
    button_jia->setMinimumHeight(50);
    button_jian->setMinimumHeight(50);
    button_cheng->setMinimumHeight(50);
    button_chu->setMinimumHeight(50);
    button_result->setMinimumHeight(50);
 
    //连接信号
    connect(button_0,SIGNAL(clicked(bool)),this,SLOT(button_0_clicked()));
    connect(button_1,SIGNAL(clicked(bool)),this,SLOT(button_1_clicked()));
    connect(button_2,SIGNAL(clicked(bool)),this,SLOT(button_2_clicked()));
    connect(button_3,SIGNAL(clicked(bool)),this,SLOT(button_3_clicked()));
    connect(button_4,SIGNAL(clicked(bool)),this,SLOT(button_4_clicked()));
    connect(button_5,SIGNAL(clicked(bool)),this,SLOT(button_5_clicked()));
    connect(button_6,SIGNAL(clicked(bool)),this,SLOT(button_6_clicked()));
    connect(button_7,SIGNAL(clicked(bool)),this,SLOT(button_7_clicked()));
    connect(button_8,SIGNAL(clicked(bool)),this,SLOT(button_8_clicked()));
    connect(button_9,SIGNAL(clicked(bool)),this,SLOT(button_9_clicked()));
    connect(button_jia,SIGNAL(clicked(bool)),this,SLOT(button_jia_clicked()));
    connect(button_jian,SIGNAL(clicked(bool)),this,SLOT(button_jian_clicked()));
    connect(button_cheng,SIGNAL(clicked(bool)),this,SLOT(button_cheng_clicked()));
    connect(button_chu,SIGNAL(clicked(bool)),this,SLOT(button_chu_clicked()));
    connect(button_ce,SIGNAL(clicked(bool)),this,SLOT(button_ce_clicked()));
    connect(button_result,SIGNAL(clicked(bool)),this,SLOT(button_result_clicked()));
 
    QGridLayout * layout = new QGridLayout;
 
    layout->addWidget(data,1,1,1,4,Qt::Alignment());
 
    layout->addWidget(button_1,2,1,Qt::Alignment());
    layout->addWidget(button_2,2,2,Qt::Alignment());
    layout->addWidget(button_3,2,3,Qt::Alignment());
    layout->addWidget(button_jia,2,4,Qt::Alignment());
 
    layout->addWidget(button_4,3,1,Qt::Alignment());
    layout->addWidget(button_5,3,2,Qt::Alignment());
    layout->addWidget(button_6,3,3,Qt::Alignment());
    layout->addWidget(button_jian,3,4,Qt::Alignment());
 
    layout->addWidget(button_7,4,1,Qt::Alignment());
    layout->addWidget(button_8,4,2,Qt::Alignment());
    layout->addWidget(button_9,4,3,Qt::Alignment());
    layout->addWidget(button_cheng,4,4,Qt::Alignment());
 
    layout->addWidget(button_ce,5,1,Qt::Alignment());
    layout->addWidget(button_0,5,2,Qt::Alignment());
    layout->addWidget(button_result,5,3,Qt::Alignment());
    layout->addWidget(button_chu,5,4,Qt::Alignment());
    layout->setMargin(12);
    centerWindow->setLayout(layout);
}
 
MainWindow::~MainWindow()
{
 
}
 
void MainWindow::init_data()
{
    num1 = 0;
    num2 = 0;
    operat_flag = 0;
}
 
void MainWindow::readNum(int num)
{
      //如果是第一个操作数
      if(operat_flag == 0){
          num1 = num1 * 10 + num;
      }
      //如果是第二个操作数
      else{
          num2 = num2 * 10 + num;
      }
}
 
void MainWindow::operation()
{
    switch(operat){
    case 1:
        result = num1 + num2;
        break;
    case 2:
        result = num1 - num2;
        break;
    case 3:
        result = num1 * num2;
        break;
    case 4:
        result = num1 / num2;
        break;
    }
    //把结果存到num1中,清零num2
    num1 = result;
    num2 = 0;
    s = QString::number(result,10);
}
 
void MainWindow::button_0_clicked(){
    s += "0";
    data->setText(s);
    readNum(0);
}
 
void MainWindow::button_1_clicked(){
    s += "1";
    data->setText(s);
    readNum(1);
}
 
void MainWindow::button_2_clicked(){
    s += "2";
    data->setText(s);
    readNum(2);
}
 
void MainWindow::button_3_clicked(){
    s += "3";
    data->setText(s);
    readNum(3);
}
 
void MainWindow::button_4_clicked(){
    s += "4";
    data->setText(s);
    readNum(4);
}
 
void MainWindow::button_5_clicked(){
    s += "5";
    data->setText(s);
    readNum(5);
}
 
void MainWindow::button_6_clicked(){
    s += "6";
    data->setText(s);
    readNum(6);
}
 
void MainWindow::button_7_clicked(){
    s += "7";
    data->setText(s);
    readNum(7);
}
 
void MainWindow::button_8_clicked(){
    s += "8";
    data->setText(s);
    readNum(8);
}
 
void MainWindow::button_9_clicked(){
    s += "9";
    data->setText(s);
    readNum(9);
}
 
void MainWindow::button_ce_clicked(){
    s = "";
    data->setText("");
    //初始化数据
    init_data();
}
 
void MainWindow::button_jia_clicked(){
    s += "+";
    data->setText(s);
    operat_flag = 1;
    operat = 1;
}
 
void MainWindow::button_jian_clicked(){
    s += "-";
    data->setText(s);
    operat_flag = 1;
    operat = 2;
}
 
void MainWindow::button_cheng_clicked(){
    s += "*";
    data->setText(s);
    operat_flag = 1;
    operat = 3;
}
 
void MainWindow::button_chu_clicked(){
    s += "/";
    data->setText(s);
    operat_flag = 1;
    operat = 4;
}
 
void MainWindow::button_result_clicked(){
    operation();
    QString a = QString::number(result,10);
    data->setText(a);
}

main.cpp

#include "mainwindow.h"
#include <QApplication>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    w.setFixedSize(400,300);
    w.setWindowTitle("计算器");
    w.myLayout();
    return a.exec();
}

演示:

99*100=

结果

继续计算/99

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯