文档章节

Qt学习之路十一(一个简易计算器)

丿咖啡灬微凉
 丿咖啡灬微凉
发布于 2017/04/18 00:00
字数 1392
阅读 32
收藏 0

  这几天一直在调试上文说到的bug,一直没有什么进展,今天我没有继续调试,而是转去做了一个简易的计算器,让我的脑子换换思路,一直想那个问题,却一直没有思路让我真的很头疼。

  好了,话不多说还是直接上代码吧,思路不难,况且我做的也只是一个简易的计算器。

/********************************************************************************
** Form generated from reading UI file 'jisuanqi.ui'
**
** Created: Sun Feb 26 21:22:25 2017
**      by: Qt User Interface Compiler version 4.8.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_JISUANQI_H
#define UI_JISUANQI_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QGridLayout>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QMainWindow>
#include <QtGui/QMenuBar>
#include <QtGui/QPushButton>
#include <QtGui/QStatusBar>
#include <QtGui/QToolBar>
#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_Jisuanqi
{
public:
    QWidget *centralWidget;
    QWidget *gridLayoutWidget;
    QGridLayout *gridLayout;
    QPushButton *num7;
    QPushButton *num8;
    QPushButton *num9;
    QPushButton *num4;
    QPushButton *num5;
    QPushButton *num6;
    QPushButton *num3;
    QPushButton *num1;
    QPushButton *num2;
    QPushButton *num0;
    QPushButton *deng;
    QPushButton *jian;
    QPushButton *chu;
    QPushButton *cheng;
    QPushButton *add;
    QPushButton *AC;
    QLabel *label;
    QMenuBar *menuBar;
    QToolBar *mainToolBar;
    QStatusBar *statusBar;

    void setupUi(QMainWindow *Jisuanqi)
    {
        if (Jisuanqi->objectName().isEmpty())
            Jisuanqi->setObjectName(QString::fromUtf8("Jisuanqi"));
        Jisuanqi->resize(300, 400);
        Jisuanqi->setMinimumSize(QSize(300, 400));
        Jisuanqi->setMaximumSize(QSize(300, 400));
        centralWidget = new QWidget(Jisuanqi);
        centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
        gridLayoutWidget = new QWidget(centralWidget);
        gridLayoutWidget->setObjectName(QString::fromUtf8("gridLayoutWidget"));
        gridLayoutWidget->setGeometry(QRect(10, 70, 271, 311));
        gridLayout = new QGridLayout(gridLayoutWidget);
        gridLayout->setSpacing(6);
        gridLayout->setContentsMargins(11, 11, 11, 11);
        gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
        gridLayout->setContentsMargins(0, 0, 0, 0);
        num7 = new QPushButton(gridLayoutWidget);
        num7->setObjectName(QString::fromUtf8("num7"));
        num7->setMinimumSize(QSize(45, 30));
        num7->setMaximumSize(QSize(45, 30));

        gridLayout->addWidget(num7, 1, 0, 1, 1);

        num8 = new QPushButton(gridLayoutWidget);
        num8->setObjectName(QString::fromUtf8("num8"));
        num8->setMinimumSize(QSize(45, 30));
        num8->setMaximumSize(QSize(45, 30));

        gridLayout->addWidget(num8, 1, 1, 1, 1);

        num9 = new QPushButton(gridLayoutWidget);
        num9->setObjectName(QString::fromUtf8("num9"));
        num9->setMinimumSize(QSize(45, 30));
        num9->setMaximumSize(QSize(45, 30));
        QFont font;
        font.setBold(false);
        font.setWeight(50);
        num9->setFont(font);

        gridLayout->addWidget(num9, 1, 2, 1, 1);

        num4 = new QPushButton(gridLayoutWidget);
        num4->setObjectName(QString::fromUtf8("num4"));
        num4->setMinimumSize(QSize(45, 30));
        num4->setMaximumSize(QSize(45, 30));

        gridLayout->addWidget(num4, 2, 0, 1, 1);

        num5 = new QPushButton(gridLayoutWidget);
        num5->setObjectName(QString::fromUtf8("num5"));
        num5->setMinimumSize(QSize(45, 30));
        num5->setMaximumSize(QSize(45, 30));

        gridLayout->addWidget(num5, 2, 1, 1, 1);

        num6 = new QPushButton(gridLayoutWidget);
        num6->setObjectName(QString::fromUtf8("num6"));
        num6->setMinimumSize(QSize(45, 30));
        num6->setMaximumSize(QSize(45, 30));

        gridLayout->addWidget(num6, 2, 2, 1, 1);

        num3 = new QPushButton(gridLayoutWidget);
        num3->setObjectName(QString::fromUtf8("num3"));
        num3->setMinimumSize(QSize(45, 30));
        num3->setMaximumSize(QSize(45, 30));

        gridLayout->addWidget(num3, 3, 2, 1, 1);

        num1 = new QPushButton(gridLayoutWidget);
        num1->setObjectName(QString::fromUtf8("num1"));
        num1->setMinimumSize(QSize(45, 30));
        num1->setMaximumSize(QSize(45, 30));

        gridLayout->addWidget(num1, 3, 0, 1, 1);

        num2 = new QPushButton(gridLayoutWidget);
        num2->setObjectName(QString::fromUtf8("num2"));
        num2->setMinimumSize(QSize(45, 30));
        num2->setMaximumSize(QSize(45, 30));

        gridLayout->addWidget(num2, 3, 1, 1, 1);

        num0 = new QPushButton(gridLayoutWidget);
        num0->setObjectName(QString::fromUtf8("num0"));
        num0->setMinimumSize(QSize(45, 30));
        num0->setMaximumSize(QSize(45, 30));

        gridLayout->addWidget(num0, 4, 0, 1, 1);

        deng = new QPushButton(gridLayoutWidget);
        deng->setObjectName(QString::fromUtf8("deng"));
        deng->setMinimumSize(QSize(45, 30));
        deng->setMaximumSize(QSize(45, 30));

        gridLayout->addWidget(deng, 4, 2, 1, 1);

        jian = new QPushButton(gridLayoutWidget);
        jian->setObjectName(QString::fromUtf8("jian"));
        jian->setMinimumSize(QSize(45, 30));
        jian->setMaximumSize(QSize(45, 30));

        gridLayout->addWidget(jian, 4, 1, 1, 1);

        chu = new QPushButton(gridLayoutWidget);
        chu->setObjectName(QString::fromUtf8("chu"));
        chu->setMinimumSize(QSize(45, 30));
        chu->setMaximumSize(QSize(45, 30));

        gridLayout->addWidget(chu, 4, 4, 1, 1);

        cheng = new QPushButton(gridLayoutWidget);
        cheng->setObjectName(QString::fromUtf8("cheng"));
        cheng->setMinimumSize(QSize(45, 30));
        cheng->setMaximumSize(QSize(45, 30));

        gridLayout->addWidget(cheng, 3, 4, 1, 1);

        add = new QPushButton(gridLayoutWidget);
        add->setObjectName(QString::fromUtf8("add"));
        add->setMinimumSize(QSize(45, 30));
        add->setMaximumSize(QSize(45, 30));

        gridLayout->addWidget(add, 2, 4, 1, 1);

        AC = new QPushButton(gridLayoutWidget);
        AC->setObjectName(QString::fromUtf8("AC"));
        AC->setMinimumSize(QSize(45, 30));
        AC->setMaximumSize(QSize(45, 30));

        gridLayout->addWidget(AC, 1, 4, 1, 1);

        label = new QLabel(centralWidget);
        label->setObjectName(QString::fromUtf8("label"));
        label->setGeometry(QRect(10, 20, 271, 61));
        QFont font1;
        font1.setPointSize(13);
        label->setFont(font1);
        label->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
        Jisuanqi->setCentralWidget(centralWidget);
        menuBar = new QMenuBar(Jisuanqi);
        menuBar->setObjectName(QString::fromUtf8("menuBar"));
        menuBar->setGeometry(QRect(0, 0, 300, 21));
        Jisuanqi->setMenuBar(menuBar);
        mainToolBar = new QToolBar(Jisuanqi);
        mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
        Jisuanqi->addToolBar(Qt::TopToolBarArea, mainToolBar);
        statusBar = new QStatusBar(Jisuanqi);
        statusBar->setObjectName(QString::fromUtf8("statusBar"));
        Jisuanqi->setStatusBar(statusBar);

        retranslateUi(Jisuanqi);

        QMetaObject::connectSlotsByName(Jisuanqi);
    } // setupUi

    void retranslateUi(QMainWindow *Jisuanqi)
    {
        Jisuanqi->setWindowTitle(QApplication::translate("Jisuanqi", "Jisuanqi", 0, QApplication::UnicodeUTF8));
        num7->setText(QApplication::translate("Jisuanqi", "7", 0, QApplication::UnicodeUTF8));
        num8->setText(QApplication::translate("Jisuanqi", "8", 0, QApplication::UnicodeUTF8));
        num9->setText(QApplication::translate("Jisuanqi", "9", 0, QApplication::UnicodeUTF8));
        num4->setText(QApplication::translate("Jisuanqi", "4", 0, QApplication::UnicodeUTF8));
        num5->setText(QApplication::translate("Jisuanqi", "5", 0, QApplication::UnicodeUTF8));
        num6->setText(QApplication::translate("Jisuanqi", "6", 0, QApplication::UnicodeUTF8));
        num3->setText(QApplication::translate("Jisuanqi", "3", 0, QApplication::UnicodeUTF8));
        num1->setText(QApplication::translate("Jisuanqi", "1", 0, QApplication::UnicodeUTF8));
        num2->setText(QApplication::translate("Jisuanqi", "2", 0, QApplication::UnicodeUTF8));
        num0->setText(QApplication::translate("Jisuanqi", "0", 0, QApplication::UnicodeUTF8));
        deng->setText(QApplication::translate("Jisuanqi", "=", 0, QApplication::UnicodeUTF8));
        jian->setText(QApplication::translate("Jisuanqi", "-", 0, QApplication::UnicodeUTF8));
        chu->setText(QApplication::translate("Jisuanqi", "/", 0, QApplication::UnicodeUTF8));
        cheng->setText(QApplication::translate("Jisuanqi", "x", 0, QApplication::UnicodeUTF8));
        add->setText(QApplication::translate("Jisuanqi", "+", 0, QApplication::UnicodeUTF8));
        AC->setText(QApplication::translate("Jisuanqi", "AC", 0, QApplication::UnicodeUTF8));
        label->setText(QString());
    } // retranslateUi

};

namespace Ui {
    class Jisuanqi: public Ui_Jisuanqi {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_JISUANQI_H

#ifndef JISUANQI_H
#define JISUANQI_H

#include <QMainWindow>

namespace Ui {
class Jisuanqi;
}

class Jisuanqi : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit Jisuanqi(QWidget *parent = 0);
    ~Jisuanqi();
    
private slots:
    void on_num0_clicked();

    void on_num1_clicked();

    void on_num2_clicked();

    void on_num3_clicked();

    void on_num4_clicked();

    void on_num5_clicked();

    void on_num6_clicked();

    void on_num7_clicked();

    void on_num8_clicked();

    void on_num9_clicked();

    void on_add_clicked();

    void on_jian_clicked();

    void on_cheng_clicked();

    void on_chu_clicked();

    void on_deng_clicked();

    void on_AC_clicked();

private:
    Ui::Jisuanqi *ui;
};

#endif // JISUANQI_H

#include "jisuanqi.h"
#include "ui_jisuanqi.h"
#include <QDebug>
#include <QStack>

QString str, str1;
double result = 0;

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

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

void Jisuanqi::on_num0_clicked()
{
    str = str + '0';
    this->ui->label->setText(str);
}

void Jisuanqi::on_num1_clicked()
{
    str = str + '1';
    this->ui->label->setText(str);
}

void Jisuanqi::on_num2_clicked()
{
    str = str + '2';
    this->ui->label->setText(str);
}

void Jisuanqi::on_num3_clicked()
{
    str = str + '3';
    this->ui->label->setText(str);
}

void Jisuanqi::on_num4_clicked()
{
    str = str + '4';
    this->ui->label->setText(str);
}

void Jisuanqi::on_num5_clicked()
{
    str = str + '5';
    this->ui->label->setText(str);
}

void Jisuanqi::on_num6_clicked()
{
    str = str + '6';
    this->ui->label->setText(str);
}

void Jisuanqi::on_num7_clicked()
{
    str = str + '7';
    this->ui->label->setText(str);
}

void Jisuanqi::on_num8_clicked()
{
    str = str + '8';
    this->ui->label->setText(str);
}

void Jisuanqi::on_num9_clicked()
{
    str = str + '9';
    this->ui->label->setText(str);
}

void Jisuanqi::on_add_clicked()
{
    str = str + '+';
    this->ui->label->setText(str);
}

void Jisuanqi::on_jian_clicked()
{
    str = str + '-';
    this->ui->label->setText(str);
}

void Jisuanqi::on_cheng_clicked()
{
    str = str + 'x';
    this->ui->label->setText(str);
}

void Jisuanqi::on_chu_clicked()
{
    str = str + '/';
    this->ui->label->setText(str);
}

void Jisuanqi::on_deng_clicked()
{
    QStack<double> num;
    QStack<char> ope;
    QString temp;
    for(int i = 0; i < str.length(); i++)
    {
        if(str[i] >= '0' && str[i] <= '9')
        {
            temp = temp + str[i];
        }
        else
        {
            num.push(temp.toDouble());
            ope.push(str[i].toAscii());
            temp = "";
        }
    }
    num.push_back(temp.toDouble());

    double num1,num2;
    char ope1;

    num1 = num.pop();
    num2 = num.pop();
    ope1 = ope.pop();
    if(ope.empty())
    {
        switch(ope1)
        {
            case '+':
            {
                result = num1 + num2;
                break;
            }
            case '-':
            {
                result = num2 - num1;
                break;
            }
            case 'x':
            {
                result = num1 * num2;
                break;
            }
            case '/':
            {
                result = num2 / num1;
                break;
            }
        }
    }

    QString str2 = QString("%1").arg(result);
    this->ui->label->setText(str2);
    str = "";
    result = 0;
}

void Jisuanqi::on_AC_clicked()
{
    str = "0";
    result = 0;
    this->ui->label->setText(str);
}

#include <QtGui/QApplication>
#include "jisuanqi.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Jisuanqi w;
    w.show();
    
    return a.exec();
}


本文转载自:http://blog.csdn.net/hjf161105/article/details/57484237

丿咖啡灬微凉
粉丝 1
博文 70
码字总数 0
作品 0
私信 提问
收藏的博客 -- Qt/C++学习

Qt下载官网 http://download.qt.io/ Qt知名社区 http://www.qtcn.org/bbs/i.php http://www.qter.org/ Qt知名博客 https://www.devbean.net/ --- Qt学习之路,Qt Creator源码学习 http://blo......

libaineu2004
2017/08/18
0
0
关于Qt5里的两个问题

第一个问题是 Qt学习之路47 里的booleanmodel.cpp文件 基类是QAbstractItemModel 有人说Qt5没有reset()函数,不知道有什么代替的。 第二个问题是 Qt学习之路48 里代码全部模仿的一样 错误: ...

李新浩
2014/03/06
244
0
《C程序设计II》简易计算器,杨辉,数字杯子图形

《C程序设计II》简易计算器,杨辉,数字杯子图形。《C程序设计II》简易计算器,杨辉,数字杯子图形。《C程序设计II》简易计算器,杨辉,数字杯子图形。 《C程序设计II》简易计算器,杨辉,数...

---dgw博客
04/14
0
0
基于QT的简易计算器_Android应用

该计算器属于Qt入门的一个实战,Qt是一个强大跨平台的框架,本项目通过Android SDK、NDK、ANT和JDK与QT作为完整的IDE进行开发,实现能在安卓8.0.0系统上跑的APP。 项目前需要配置安卓编译环境...

LoSingSang
2018/07/01
124
2
Qt 学习之路 2(52):使用拖放

拖放(Drag and Drop),通常会简称为 DnD,是现代软件开发中必不可少的一项技术。它提供了一种能够在应用程序内部甚至是应用程序之间进行信息交换的机制。操作系统与应用程序之间进行的剪贴...

SuShine
2016/05/12
109
0

没有更多内容

加载失败,请刷新页面

加载更多

CentOS7.6中安装使用fcitx框架

内容目录 一、为什么要使用fcitx?二、安装fcitx框架三、安装搜狗输入法 一、为什么要使用fcitx? Gnome3桌面自带的输入法框架为ibus,而在使用ibus时会时不时出现卡顿无法输入的现象。 搜狗和...

技术训练营
昨天
5
0
《Designing.Data-Intensive.Applications》笔记 四

第九章 一致性与共识 分布式系统最重要的的抽象之一是共识(consensus):让所有的节点对某件事达成一致。 最终一致性(eventual consistency)只提供较弱的保证,需要探索更高的一致性保证(stro...

丰田破产标志
昨天
8
0
docker 使用mysql

1, 进入容器 比如 myslq1 里面进行操作 docker exec -it mysql1 /bin/bash 2. 退出 容器 交互: exit 3. mysql 启动在容器里面,并且 可以本地连接mysql docker run --name mysql1 --env MY...

之渊
昨天
10
0
python数据结构

1、字符串及其方法(案例来自Python-100-Days) def main(): str1 = 'hello, world!' # 通过len函数计算字符串的长度 print(len(str1)) # 13 # 获得字符串首字母大写的...

huijue
昨天
6
0
PHP+Ajax微信手机端九宫格抽奖实例

PHP+Ajax结合lottery.js制作的一款微信手机端九宫格抽奖实例,抽奖完成后有收货地址添加表单出现。支持可以设置中奖概率等。 奖品列表 <div class="lottery_list clearfix" id="lottery"> ......

ymkjs1990
昨天
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部