qt下实现一个简单的计算器

在qt下实现一个简单的计算器,步骤如下:
第一步:1.新建Qt工程,项目名称根据你的习惯命名,类名为MainWindow,基类为QMainWindow。
qt下实现一个简单的计算器_第1张图片
第二步:完成项目创建后,在类中定义要用到的控件等控件,mainwindow.h具体代码如下:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 
#include "QAction"
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
public:
    //显示标签
        QLabel * label1;
        QLabel * label2;
        //文本编辑框
        QTextEdit * showEdit;
        QTextEdit * resultEdit;
        //按钮布局成员变量
        QPushButton * backBt;
        QPushButton * removeBt;
        QPushButton * clearBt;
        QPushButton * sumBt;

        QPushButton * sevenBt;
        QPushButton * eightBt;
        QPushButton * nineBt;
        QPushButton * decBt;

        QPushButton * fourBt;
        QPushButton * fiveBt;
        QPushButton * sixBt;
        QPushButton * starBt;

        QPushButton * oneBt;
        QPushButton * twoBt;
        QPushButton * threeBt;
        QPushButton * divBt;

        QPushButton * _Bt;  //+/-按钮
        QPushButton * zeroBt;
        QPushButton * pointBt;
        QPushButton * equalBt;
        QMenu *menu1;
        QMenu *menu2;
        QMenu *menu3;
        QAction *action1;
        QAction *action2;
        QAction *action3;
        QAction *action4;
        QAction *action5;
        QAction *action6;
        QAction *action7;
        QAction *action8;
        
           




public:
            //布局管理函数
        void mylayout();
        //增加菜单函数
        void addMenu();
        //增加菜单栏选项函数
        void addMenuAction();
    
private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

第三步,在mainwindow.cpp中实现在头文件中声明的三个函数,代码如下:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QHBoxLayout"
#include "QAction"
#include "QMessageBox"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    mylayout();
    addMenuAction();
    addMenu();
}
void MainWindow::mylayout()
{
    //设置中心窗口
    QWidget *centerWindow = new QWidget;
    this->setCentralWidget(centerWindow);

    label1 = new QLabel("显示");
    label2 = new QLabel("结果");
    QHBoxLayout * h1 = new QHBoxLayout;
    h1->addWidget(label1);
    h1->addWidget(label2);
    label1->setMaximumHeight(15);
    label2->setMaximumHeight(15);
    h1->setStretch(0,2);
    h1->setStretch(1,1);

   showEdit = new QTextEdit;
    resultEdit = new QTextEdit;
    QHBoxLayout * h2 = new QHBoxLayout;
    h2->addWidget(showEdit);
    h2->addWidget(resultEdit);
    showEdit->setMaximumHeight(30);
    resultEdit->setMaximumHeight(30);
    showEdit->setReadOnly(true);
    resultEdit->setReadOnly(true);
    h2->setStretch(0,2);
    h2->setStretch(1,1);

    //第三行按钮
    backBt   = new QPushButton("Back");
    removeBt = new QPushButton("CE");
    clearBt  = new QPushButton("CLS");
    sumBt    = new QPushButton("+");
    backBt->setMinimumHeight(50);
    removeBt->setMinimumHeight(50);
    clearBt->setMinimumHeight(50);
    sumBt->setMinimumHeight(50);
    QHBoxLayout * h3 = new QHBoxLayout;
    h3->addWidget(backBt);
    h3->addWidget(removeBt);
    h3->addWidget(clearBt);
    h3->addWidget(sumBt);
    sevenBt = new QPushButton("7");
    eightBt = new QPushButton("8");
    nineBt  = new QPushButton("9");
    decBt   = new QPushButton("-");
    sevenBt->setMinimumHeight(50);
    eightBt->setMinimumHeight(50);
    nineBt->setMinimumHeight(50);
    decBt->setMinimumHeight(50);
    QHBoxLayout * h4 = new QHBoxLayout;
    h4->addWidget(sevenBt);
    h4->addWidget(eightBt);
    h4->addWidget(nineBt);
    h4->addWidget(decBt);

    fourBt = new QPushButton("4");
    fiveBt = new QPushButton("5");
    sixBt  = new QPushButton("6");
    starBt = new QPushButton("*");
    fourBt->setMinimumHeight(50);
    fiveBt->setMinimumHeight(50);
    sixBt->setMinimumHeight(50);
    starBt->setMinimumHeight(50);
    QHBoxLayout * h5 = new QHBoxLayout;
    h5->addWidget(fourBt);
    h5->addWidget(fiveBt);
    h5->addWidget(sixBt);
    h5->addWidget(starBt);

    oneBt    = new QPushButton("1");
    twoBt    = new QPushButton("2");
    threeBt  = new QPushButton("3");
    divBt    = new QPushButton("/");
    oneBt->setMinimumHeight(50);
    twoBt->setMinimumHeight(50);
    threeBt->setMinimumHeight(50);
    divBt->setMinimumHeight(50);
    QHBoxLayout * h6 = new QHBoxLayout;
    h6->addWidget(oneBt);
    h6->addWidget(twoBt);
    h6->addWidget(threeBt);
    h6->addWidget(divBt);

    _Bt      =  new QPushButton("+/-");
    zeroBt   =  new QPushButton("0");
    pointBt  =  new QPushButton(".");
    equalBt  =  new QPushButton("=");
    _Bt->setMinimumHeight(50);
    zeroBt->setMinimumHeight(50);
    pointBt->setMinimumHeight(50);
    equalBt->setMinimumHeight(50);
    QHBoxLayout * h7 = new QHBoxLayout;
    h7->addWidget(_Bt);
    h7->addWidget(zeroBt);
    h7->addWidget(pointBt);
    h7->addWidget(equalBt);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(h1);
    mainLayout->addLayout(h2);
    mainLayout->addLayout(h3);
    mainLayout->addLayout(h4);
    mainLayout->addLayout(h5);
    mainLayout->addLayout(h6);
    mainLayout->addLayout(h7);

    centerWindow->setLayout(mainLayout);
 }
void MainWindow::addMenu()
{

    menu1=menuBar()->addMenu("选择");
    menu1->addAction(action1);
    menu1->addAction(action2);
    //增加横线
    menu1->addSeparator();
    menu1->addAction(action3);
    menu1->addAction(action4);

    menu2=menuBar()->addMenu("编辑");
    menu2->addAction(action5);
    menu2->addSeparator();
    menu2->addAction(action6);

    menu3=menuBar()->addMenu("帮助");
    menu3->addAction(action7);
    menu3->addSeparator();
    menu3->addAction(action8);
}
//增加菜单栏选项
void MainWindow::addMenuAction()
{
    action1 = new QAction("标准",this);
   action1->setShortcut(tr("alt+l"));
    action2 = new QAction("科学",this);
    action2->setShortcut(tr("alt+2"));
    action3 = new QAction("历史记录",this);
    action3->setShortcut(tr("alt+3"));
    action4 = new QAction("时间设置",this);
    action4->setShortcut(tr("alt+4"));
    action5 = new QAction("复制",this);
    action5->setShortcut(tr("ctrl+1"));
    action6 = new QAction("粘贴",this);
    action6->setShortcut(tr("ctrl+2"));
    action7 = new QAction("帮助",this);
    action7->setShortcut(tr("ctrl+3"));

    action8 = new QAction("关于",this);
    action8->setShortcut(tr("ctrl+4"));
}
MainWindow::~MainWindow()
{
    delete ui;
}

实现结果如下:
qt下实现一个简单的计算器_第2张图片
第四步:添加计算器中每一个按钮的槽函数,在mainwindow.h中声明如下槽函数和函数:

void JudgeZero();
        void BeforeOperator(int iOperator);
        void init();
        void keyPressEvent(QKeyEvent * e);
private slots:
    //点击关于槽函数
    void onClickAbort();
    //点击帮助槽函数
    void onClickHelp();
    //点击复制槽函数
    void onClickCopy();
    //点击粘贴槽函数
    void onClickPaste();
    void onPushBtone();
    void onPushBttwo();
    void onPushBtthree();
    void onPushBtdiv();
    void onPushBtBack();
    void onPushBtRemove();
    void onPushBtClear();
    void onPushBtSum();
    void onPushBtSeven();
    void onPushBtEight();
    void onPushBtNine();
    void onPushBtDec();
    void onPushBtMul();
    void onPushBtFour();
    void onPushBtFive();
    void onPushBtSix();
    void onPushBtSign();
    void onPushBtPoint();
    void onPushBtZero();
    void onPushBtEqual();

在mainwindow.cpp中函数mylayout中添加信号与槽的连接,添加代码如下:

connect(backBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtBack()));
    connect(removeBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtRemove()));
    connect(clearBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtClear()));
    connect(sumBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtSum()));
    connect(sevenBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtSeven()));
    connect(eightBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtEight()));
    connect(nineBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtNine()));
    connect(decBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtDec()));
    connect(fourBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtFour()));
    connect(fiveBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtFive()));
    connect(sixBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtSix()));
    connect(starBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtMul()));
    connect(oneBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtone()));
    connect(twoBt,SIGNAL(clicked(bool)),this,SLOT(onPushBttwo()));
    connect(threeBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtthree()));
    connect(divBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtdiv()));
    connect(_Bt,SIGNAL(clicked(bool)),this,SLOT(onPushBtSign()));
    connect(pointBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtPoint()));
    connect(equalBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtEqual()));
    connect(zeroBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtZero()));

第五步:在头文件mainwindow.h中添加变量用于存储操作数及结果。

//显示结果
    qreal result;
    //第一个操作数
    qreal firstOpeNum;
    //第二个操作数
    qreal secondOpeNum;
    //显示字符串
    QString contextStr;
    int operat;   //操作符

部分函数和槽函数实现代码如下:
函数onPushBtone

//按键槽函数
void MainWindow::onPushBtone()
{
    JudgeZero();
    contextStr = contextStr + "1";
    showEdit->setText(contextStr);
}

其他数字按键代码槽函数跟以上代码类似,代码4-4中函数JudgeZero()是用来防止输入数字是开头是两个0,代码如下:

//函数JudgeZero
void MainWindow::JudgeZero()
{
    if(contextStr.size() == 1)
    {
        if(contextStr.contains("0"))
        {
            contextStr.clear();
        }
    }
}

实现操作符(+,-,*,/)槽函数,以下是+的槽函数onPushBtSum的代码:
函数onPushBtSum

     void MainWindow::onPushBtSum()
{
    BeforeOperator(ADD);
}

其中函数BeforeOperator代码如下:

//在按+——*/之前需要的操作
void MainWindow::BeforeOperator(int iOperator)
{
    if(contextStr.size() == 0 || contextStr == "-")
    {
        return;
    }
    if(operat == 0)
    {
         firstOpeNum = contextStr.toDouble();
    }
    operat = iOperator;
    contextStr.clear();
    showEdit->setText(contextStr);
}

=键的槽函数onPushBtEqual代码如下:

void MainWindow::onPushBtEqual()
{
    QString strTem;
    if(contextStr.size() == 0||contextStr == "-")
    {
        return;
    }
    //第一步,先将数据给第二个操作数
     secondOpeNum = contextStr.toDouble();
     switch(operat)
     {
        case 0:
        break;
        case add:
        {
            result = secondOpeNum + firstOpeNum;
            strTem = QString::number(result);
            resultEdit->setText(strTem);
        }
         break;
         case dec:
        {
             result =  firstOpeNum -secondOpeNum ;
             strTem = QString::number(result);
            resultEdit->setText(strTem);
         }
         break;
        case mul:
        {
             result = secondOpeNum*firstOpeNum;
            strTem = QString::number(result);
            resultEdit->setText(strTem);
        }
         break;
        case div:
        {
            if(secondOpeNum*2 == 0)
            {
                QMessageBox::information(this,"错误","被除数不能为0");
                return;
            }
            result = firstOpeNum/secondOpeNum;
            strTem = QString::number(result);
            resultEdit->setText(strTem);
         }
         break;
     }
     init();
     contextStr.clear();
     showEdit->setText(contextStr);

}

第六步:实现按键功能,其代码如下:

//按键时间响应函数
void MainWindow::keyPressEvent(QKeyEvent * e)
{
    int iKey = e->key();
    switch(iKey)
    {
        case Qt::Key_0:onPushBtZero(); break;
        case Qt::Key_1:onPushBtone(); break;
        case Qt::Key_2:onPushBttwo(); break;
        case Qt::Key_3:onPushBtthree(); break;
        case Qt::Key_4:onPushBtFour(); break;
        case Qt::Key_5:onPushBtFive(); break;
        case Qt::Key_6:onPushBtSix(); break;
        case Qt::Key_7:onPushBtSeven(); break;
        case Qt::Key_8:onPushBtEight(); break;
        case Qt::Key_9:onPushBtNine(); break;
        case Qt::Key_Backspace:onPushBtBack();break;
        case Qt::Key_Equal:onPushBtEqual();break;
        case Qt::Key_Plus:onPushBtSum();break;
        case Qt::Key_Period:onPushBtPoint();break;
        case Qt::Key_Minus:onPushBtDec();break;
        case Qt::Key_Asterisk:onPushBtMul();break;
        case Qt::Key_Slash:onPushBtdiv();break;
    }
}

项目的完整代码如下:
mainwindow.h代码

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 
#include "QAction"
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
public:
    //显示标签
        QLabel * label1;
        QLabel * label2;
        //文本编辑框
        QTextEdit * showEdit;
        QTextEdit * resultEdit;
        //按钮布局成员变量
        QPushButton * backBt;
        QPushButton * removeBt;
        QPushButton * clearBt;
        QPushButton * sumBt;

        QPushButton * sevenBt;
        QPushButton * eightBt;
        QPushButton * nineBt;
        QPushButton * decBt;

        QPushButton * fourBt;
        QPushButton * fiveBt;
        QPushButton * sixBt;
        QPushButton * starBt;

        QPushButton * oneBt;
        QPushButton * twoBt;
        QPushButton * threeBt;
        QPushButton * divBt;

        QPushButton * _Bt;  //+/-按钮
        QPushButton * zeroBt;
        QPushButton * pointBt;
        QPushButton * equalBt;
        QMenu *menu1;
        QMenu *menu2;
        QMenu *menu3;
        QAction *action1;
        QAction *action2;
        QAction *action3;
        QAction *action4;
        QAction *action5;
        QAction *action6;
        QAction *action7;
        QAction *action8;
        //显示结果
            qreal result;
            //第一个操作数
            qreal firstOpeNum;
            //第二个操作数
            qreal secondOpeNum;
            //显示字符串
            QString contextStr;
            int operat = 0;   //操作符
            int const ADD = 1;
            int const DEC = 2;
            int const MUL = 3;
            int const DIV = 4;
            int edittext;




public:
        void mylayout();
        void addMenu();
        void addMenuAction();
        void JudgeZero();
        void BeforeOperator(int iOperator);
        void init();
        void keyPressEvent(QKeyEvent * e);
private slots:
    //点击关于槽函数
    void onClickAbort();
    //点击帮助槽函数
    void onClickHelp();
    //点击复制槽函数
    void onClickCopy();
    //点击粘贴槽函数
    void onClickPaste();
    void onPushBtone();
    void onPushBttwo();
    void onPushBtthree();
    void onPushBtdiv();
    void onPushBtBack();
    void onPushBtRemove();
    void onPushBtClear();
    void onPushBtSum();
    void onPushBtSeven();
    void onPushBtEight();
    void onPushBtNine();
    void onPushBtDec();
    void onPushBtMul();
    void onPushBtFour();
    void onPushBtFive();
    void onPushBtSix();
    void onPushBtSign();
    void onPushBtPoint();
    void onPushBtZero();
    void onPushBtEqual();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp代码如下:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QHBoxLayout"
#include "QAction"
#include "QMessageBox"
#include "QDebug"

#include
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    mylayout();
    addMenuAction();
    addMenu();

}
void MainWindow::mylayout()
{
    //设置中心窗口
    QWidget *centerWindow = new QWidget;
    this->setCentralWidget(centerWindow);

    label1 = new QLabel("显示");
    label2 = new QLabel("结果");
    QHBoxLayout * h1 = new QHBoxLayout;
    h1->addWidget(label1);
    h1->addWidget(label2);
    label1->setMaximumHeight(15);
    label2->setMaximumHeight(15);
    h1->setStretch(0,2);
    h1->setStretch(1,1);

   showEdit = new QTextEdit;
    resultEdit = new QTextEdit;
    QHBoxLayout * h2 = new QHBoxLayout;
    h2->addWidget(showEdit);
    h2->addWidget(resultEdit);
    showEdit->setMaximumHeight(30);
    resultEdit->setMaximumHeight(30);
    showEdit->setReadOnly(true);
    resultEdit->setReadOnly(true);
    h2->setStretch(0,2);
    h2->setStretch(1,1);

    //第三行按钮
    backBt   = new QPushButton("Back");
    removeBt = new QPushButton("CE");
    clearBt  = new QPushButton("CLS");
    sumBt    = new QPushButton("+");
    backBt->setMinimumHeight(50);
    removeBt->setMinimumHeight(50);
    clearBt->setMinimumHeight(50);
    sumBt->setMinimumHeight(50);
    QHBoxLayout * h3 = new QHBoxLayout;
    h3->addWidget(backBt);
    h3->addWidget(removeBt);
    h3->addWidget(clearBt);
    h3->addWidget(sumBt);
    sevenBt = new QPushButton("7");
    eightBt = new QPushButton("8");
    nineBt  = new QPushButton("9");
    decBt   = new QPushButton("-");
    sevenBt->setMinimumHeight(50);
    eightBt->setMinimumHeight(50);
    nineBt->setMinimumHeight(50);
    decBt->setMinimumHeight(50);
    QHBoxLayout * h4 = new QHBoxLayout;
    h4->addWidget(sevenBt);
    h4->addWidget(eightBt);
    h4->addWidget(nineBt);
    h4->addWidget(decBt);

    fourBt = new QPushButton("4");
    fiveBt = new QPushButton("5");
    sixBt  = new QPushButton("6");
    starBt = new QPushButton("*");
    fourBt->setMinimumHeight(50);
    fiveBt->setMinimumHeight(50);
    sixBt->setMinimumHeight(50);
    starBt->setMinimumHeight(50);
    QHBoxLayout * h5 = new QHBoxLayout;
    h5->addWidget(fourBt);
    h5->addWidget(fiveBt);
    h5->addWidget(sixBt);
    h5->addWidget(starBt);

    oneBt    = new QPushButton("1");
    twoBt    = new QPushButton("2");
    threeBt  = new QPushButton("3");
    divBt    = new QPushButton("/");
    oneBt->setMinimumHeight(50);
    twoBt->setMinimumHeight(50);
    threeBt->setMinimumHeight(50);
    divBt->setMinimumHeight(50);
    QHBoxLayout * h6 = new QHBoxLayout;
    h6->addWidget(oneBt);
    h6->addWidget(twoBt);
    h6->addWidget(threeBt);
    h6->addWidget(divBt);

    _Bt      =  new QPushButton("+/-");
    zeroBt   =  new QPushButton("0");
    pointBt  =  new QPushButton(".");
    equalBt  =  new QPushButton("=");
    _Bt->setMinimumHeight(50);
    zeroBt->setMinimumHeight(50);
    pointBt->setMinimumHeight(50);
    equalBt->setMinimumHeight(50);
    QHBoxLayout * h7 = new QHBoxLayout;
    h7->addWidget(_Bt);
    h7->addWidget(zeroBt);
    h7->addWidget(pointBt);
    h7->addWidget(equalBt);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(h1);
    mainLayout->addLayout(h2);
    mainLayout->addLayout(h3);
    mainLayout->addLayout(h4);
    mainLayout->addLayout(h5);
    mainLayout->addLayout(h6);
    mainLayout->addLayout(h7);

    centerWindow->setLayout(mainLayout);
    connect(backBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtBack()));
        connect(removeBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtRemove()));
        connect(clearBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtClear()));
        connect(sumBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtSum()));
        connect(sevenBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtSeven()));
        connect(eightBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtEight()));
        connect(nineBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtNine()));
        connect(decBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtDec()));
        connect(fourBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtFour()));
        connect(fiveBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtFive()));
        connect(sixBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtSix()));
        connect(starBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtMul()));
        connect(oneBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtone()));
        connect(twoBt,SIGNAL(clicked(bool)),this,SLOT(onPushBttwo()));
        connect(threeBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtthree()));
        connect(divBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtdiv()));
        connect(_Bt,SIGNAL(clicked(bool)),this,SLOT(onPushBtSign()));
        connect(pointBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtPoint()));
        connect(equalBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtEqual()));
        connect(zeroBt,SIGNAL(clicked(bool)),this,SLOT(onPushBtZero()));


}
void MainWindow::addMenu()
{

    menu1=menuBar()->addMenu("选择");
    menu1->addAction(action1);
    menu1->addAction(action2);
    //增加横线
    menu1->addSeparator();
    menu1->addAction(action3);
    menu1->addAction(action4);

    menu2=menuBar()->addMenu("编辑");
    menu2->addAction(action5);
    menu2->addSeparator();
    menu2->addAction(action6);

    menu3=menuBar()->addMenu("帮助");
    menu3->addAction(action7);
    menu3->addSeparator();
    menu3->addAction(action8);
}
//增加菜单栏选项
void MainWindow::addMenuAction()
{
    action1 = new QAction("标准",this);
   action1->setShortcut(tr("alt+l"));
    action2 = new QAction("科学",this);
    action2->setShortcut(tr("alt+2"));
    action3 = new QAction("历史记录",this);
    action3->setShortcut(tr("alt+3"));
    action4 = new QAction("时间设置",this);
    action4->setShortcut(tr("alt+4"));
    action5 = new QAction("复制",this);
    action5->setShortcut(tr("ctrl+1"));
    action6 = new QAction("粘贴",this);
    action6->setShortcut(tr("ctrl+2"));
    action7 = new QAction("帮助",this);
    action7->setShortcut(tr("ctrl+3"));

    action8 = new QAction("关于",this);
    action8->setShortcut(tr("ctrl+4"));
}
void MainWindow::BeforeOperator(int iOperator)
{
    if(contextStr.size() == 0 || contextStr == "-")
    {
        return;
    }
    if(operat == 0)
    {
         firstOpeNum = contextStr.toDouble();
    }
    operat = iOperator;
    contextStr.clear();
    showEdit->setText(contextStr);
}

void MainWindow::init()
{
     firstOpeNum = 0;
    //第二个操作数
    secondOpeNum = 0;
    operat = 0;

}
void MainWindow::onClickAbort()
{

}

void MainWindow::onClickHelp()
{

}

void MainWindow::onClickCopy()
{

}

void MainWindow::onClickPaste()
{

}

void MainWindow::onPushBtone()
{
    JudgeZero();
      contextStr = contextStr + "1";
      showEdit->setText(contextStr);

}

void MainWindow::onPushBttwo()
{
    JudgeZero();
      contextStr = contextStr + "2";
      showEdit->setText(contextStr);

}

void MainWindow::onPushBtthree()
{
    JudgeZero();
      contextStr = contextStr + "3";
      showEdit->setText(contextStr);

}

void MainWindow::onPushBtdiv()
{
    BeforeOperator(DIV);

}

void MainWindow::onPushBtBack()
{

}

void MainWindow::onPushBtRemove()
{
    edittext = contextStr.toInt();
    //qDebug()<setText(contextStr);



}

void MainWindow::onPushBtClear()
{
    contextStr.clear();
    showEdit->setText(contextStr);

}

void MainWindow::onPushBtSum()
{
    BeforeOperator(ADD);

}

void MainWindow::onPushBtSeven()
{
    JudgeZero();
      contextStr = contextStr + "7";
      showEdit->setText(contextStr);

}

void MainWindow::onPushBtEight()
{
    JudgeZero();
      contextStr = contextStr + "8";
      showEdit->setText(contextStr);

}

void MainWindow::onPushBtNine()
{
    JudgeZero();
      contextStr = contextStr + "9";
      showEdit->setText(contextStr);

}

void MainWindow::onPushBtDec()
{
    BeforeOperator(DEC);

}

void MainWindow::onPushBtMul()
{
    BeforeOperator(MUL);

}

void MainWindow::onPushBtFour()
{
    JudgeZero();
      contextStr = contextStr + "4";
      showEdit->setText(contextStr);

}

void MainWindow::onPushBtFive()
{
    JudgeZero();
      contextStr = contextStr + "5";
      showEdit->setText(contextStr);

}

void MainWindow::onPushBtSix()
{
    JudgeZero();
      contextStr = contextStr + "6";
      showEdit->setText(contextStr);

}

void MainWindow::onPushBtSign()
{

}

void MainWindow::onPushBtPoint()
{
    JudgeZero();
      contextStr = contextStr + ".";
      showEdit->setText(contextStr);

}

void MainWindow::onPushBtZero()
{
    JudgeZero();
      contextStr = contextStr + "0";
      showEdit->setText(contextStr);

}

void MainWindow::onPushBtEqual()
{
    QString strTem;
       if(contextStr.size() == 0||contextStr == "-")
       {
           return;
       }
       //第一步,先将数据给第二个操作数
        secondOpeNum = contextStr.toDouble();
        int const add = 1;
        int const dec = 2;
        int const mul= 3;
        int const div = 4;
        switch(operat)
        {


           case 0:
           break;
           case add:
           {
               result = secondOpeNum + firstOpeNum;
               strTem = QString::number(result);
               resultEdit->setText(strTem);
           }
            break;
            case dec:
           {
                result =  firstOpeNum -secondOpeNum ;
                strTem = QString::number(result);
               resultEdit->setText(strTem);
            }
            break;
           case mul:
           {
                result = secondOpeNum*firstOpeNum;
               strTem = QString::number(result);
               resultEdit->setText(strTem);
           }
            break;
           case div:
           {
               if(secondOpeNum*2 == 0)
               {
                   QMessageBox::information(this,"错误","被除数不能为0");
                   return;
               }
               result = firstOpeNum/secondOpeNum;
               strTem = QString::number(result);
               resultEdit->setText(strTem);
            }
            break;
        }
        init();
        contextStr.clear();
        showEdit->setText(contextStr);

}
void MainWindow::JudgeZero()
{
    if(contextStr.size() == 1)
    {
        if(contextStr.contains("0"))
        {
            contextStr.clear();
        }
    }
}
//按键时间响应函数
void MainWindow::keyPressEvent(QKeyEvent * e)
{
    int iKey = e->key();
    switch(iKey)
    {
        case Qt::Key_0:onPushBtZero(); break;
        case Qt::Key_1:onPushBtone(); break;
        case Qt::Key_2:onPushBttwo(); break;
        case Qt::Key_3:onPushBtthree(); break;
        case Qt::Key_4:onPushBtFour(); break;
        case Qt::Key_5:onPushBtFive(); break;
        case Qt::Key_6:onPushBtSix(); break;
        case Qt::Key_7:onPushBtSeven(); break;
        case Qt::Key_8:onPushBtEight(); break;
        case Qt::Key_9:onPushBtNine(); break;
        case Qt::Key_Backspace:onPushBtBack();break;
        case Qt::Key_Equal:onPushBtEqual();break;
        case Qt::Key_Plus:onPushBtSum();break;
        case Qt::Key_Period:onPushBtPoint();break;
        case Qt::Key_Minus:onPushBtDec();break;
        case Qt::Key_Asterisk:onPushBtMul();break;
        case Qt::Key_Slash:onPushBtdiv();break;
    }
}
MainWindow::~MainWindow()
{
    delete ui;
}

main.cpp代码如下:

#include "mainwindow.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

运行项目,计算器可进行简单的计算。

你可能感兴趣的:(编程知识,Qt,基础,计算器)