QT制作计算器终极版

2 篇文章 0 订阅

UI和main函数跟上一篇一样

下面是计算器接口和实现

jsuanqi.h

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   Jisuanqi.h
 * 
 *
 * Created on 2018年8月18日, 上午12:01
 */

#ifndef JISUANQI_H
#define JISUANQI_H
#include "ui_jisuanqi.h"

class Jisuanqi : public QMainWindow {
    Q_OBJECT
public:
    Jisuanqi();
    virtual ~Jisuanqi();
    
    void iniSignalSolts();//信号与槽关联
    
    void calculate();//计算
    
    void input();//接受输入
    
public slots:
    void textChanged(); // 显示框槽函数
    
    void OnClickedpb0();//按键0
    void OnClickedpb1();
    void OnClickedpb2();
    void OnClickedpb3();
    void OnClickedpb4();
    void OnClickedpb5();
    void OnClickedpb6();
    void OnClickedpb7();
    void OnClickedpb8();
    void OnClickedpb9();
    void OnClickedpbf0();// .
    void OnClickedpbf1();// =
    void OnClickedpbf2();// +
    void OnClickedpbf3();// -
    void OnClickedpbf4();// *
    void OnClickedpbf5();// /
    void OnClickedpbf6();// +/-
    void OnClickedpbf7();// C

private:
    Ui::MainWindow widget;

    char yunsuanfu;
    double d1;
    double d2;
    double d3;//结果
    bool dmaking;//输入数字状态
    bool d1ready;//d1就绪
    QString digit;//输入的数字
    QString rslt;//显示的结果
};


#endif /* JISUANQI_H */

jisuanqi.cpp

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

#include "jisuanqi.h"

Jisuanqi::Jisuanqi() {
    widget.setupUi(this);
    widget.lineEdit->setText("0");

    rslt = "0";
    digit = "";
    yunsuanfu = ' ';
    dmaking = false;
    d1ready = false;
    d1 = 0;
    d2 = 0;
    d3 = 0;


    iniSignalSolts();
}

Jisuanqi::~Jisuanqi() {
}

void Jisuanqi::iniSignalSolts() {
    connect(widget.pbf0, SIGNAL(clicked()), this, SLOT(OnClickedpbf0()));
    connect(widget.pbf1, SIGNAL(clicked()), this, SLOT(OnClickedpbf1()));
    connect(widget.pbf2, SIGNAL(clicked()), this, SLOT(OnClickedpbf2()));
    connect(widget.pbf3, SIGNAL(clicked()), this, SLOT(OnClickedpbf3()));
    connect(widget.pbf4, SIGNAL(clicked()), this, SLOT(OnClickedpbf4()));
    connect(widget.pbf5, SIGNAL(clicked()), this, SLOT(OnClickedpbf5()));
    connect(widget.pbf6, SIGNAL(clicked()), this, SLOT(OnClickedpbf6()));
    connect(widget.pbf7, SIGNAL(clicked()), this, SLOT(OnClickedpbf7()));

    connect(widget.pbf0, SIGNAL(clicked()), this, SLOT(textChanged()));
    connect(widget.pbf1, SIGNAL(clicked()), this, SLOT(textChanged()));
    connect(widget.pbf2, SIGNAL(clicked()), this, SLOT(textChanged()));
    connect(widget.pbf3, SIGNAL(clicked()), this, SLOT(textChanged()));
    connect(widget.pbf4, SIGNAL(clicked()), this, SLOT(textChanged()));
    connect(widget.pbf5, SIGNAL(clicked()), this, SLOT(textChanged()));
    connect(widget.pbf6, SIGNAL(clicked()), this, SLOT(textChanged()));
    connect(widget.pbf7, SIGNAL(clicked()), this, SLOT(textChanged()));

    connect(widget.pb0, SIGNAL(clicked()), this, SLOT(OnClickedpb0()));
    connect(widget.pb1, SIGNAL(clicked()), this, SLOT(OnClickedpb1()));
    connect(widget.pb2, SIGNAL(clicked()), this, SLOT(OnClickedpb2()));
    connect(widget.pb3, SIGNAL(clicked()), this, SLOT(OnClickedpb3()));
    connect(widget.pb4, SIGNAL(clicked()), this, SLOT(OnClickedpb4()));
    connect(widget.pb5, SIGNAL(clicked()), this, SLOT(OnClickedpb5()));
    connect(widget.pb6, SIGNAL(clicked()), this, SLOT(OnClickedpb6()));
    connect(widget.pb7, SIGNAL(clicked()), this, SLOT(OnClickedpb7()));
    connect(widget.pb8, SIGNAL(clicked()), this, SLOT(OnClickedpb8()));
    connect(widget.pb9, SIGNAL(clicked()), this, SLOT(OnClickedpb9()));

    connect(widget.pb0, SIGNAL(clicked()), this, SLOT(textChanged()));
    connect(widget.pb1, SIGNAL(clicked()), this, SLOT(textChanged()));
    connect(widget.pb2, SIGNAL(clicked()), this, SLOT(textChanged()));
    connect(widget.pb3, SIGNAL(clicked()), this, SLOT(textChanged()));
    connect(widget.pb4, SIGNAL(clicked()), this, SLOT(textChanged()));
    connect(widget.pb5, SIGNAL(clicked()), this, SLOT(textChanged()));
    connect(widget.pb6, SIGNAL(clicked()), this, SLOT(textChanged()));
    connect(widget.pb7, SIGNAL(clicked()), this, SLOT(textChanged()));
    connect(widget.pb8, SIGNAL(clicked()), this, SLOT(textChanged()));
    connect(widget.pb9, SIGNAL(clicked()), this, SLOT(textChanged()));

}

void Jisuanqi::calculate() {
    if (dmaking && d1ready) {
        switch (yunsuanfu) {
            case '+':
                d2 = rslt.toDouble();
                d3 = d1 + d2;
                break;
            case '-':
                d2 = rslt.toDouble();
                d3 = d1 - d2;
                break;
            case '*':
                d2 = rslt.toDouble();
                d3 = d1 * d2;
                break;
            case '/':
                d2 = rslt.toDouble();
                if (d2 != 0) {
                    d3 = d1 / d2;
                } else {
                    d3 = 0;
                }
                break;
            default:
                //break;
                return;
        }
        
        QString qst = QString::number((d3), 'f'); //QString::number((d3), 'f',4)保留4位小数

        //去掉小数末尾的0/.
        //原型bool endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
        //第二个参数表示是否区分大小写
        if (qst.contains("."))
            while (qst.endsWith("0")) {
                qst.remove(qst.length() - 1, 1);
            }
        if (qst.endsWith("."))
            qst.remove(qst.length() - 1, 1);
        rslt = qst;
    }
}

void Jisuanqi::input() {
    if (rslt != "0" && dmaking)
        rslt += digit;
    else {
        rslt = digit;
        dmaking = true;
    }
}

void Jisuanqi::textChanged() {
    widget.lineEdit->setText(rslt);
}

void Jisuanqi::OnClickedpb0() {
    digit = widget.pb0 ->text();
    input();
}

void Jisuanqi::OnClickedpb1() {
    digit = widget.pb1 ->text();
    input();
}

void Jisuanqi::OnClickedpb2() {
    digit = widget.pb2 ->text();
    input();
}

void Jisuanqi::OnClickedpb3() {
    digit = widget.pb3 ->text();
    input();
}

void Jisuanqi::OnClickedpb4() {
    digit = widget.pb4 ->text();
    input();
}

void Jisuanqi::OnClickedpb5() {
    digit = widget.pb5 ->text();
    input();
}

void Jisuanqi::OnClickedpb6() {
    digit = widget.pb6 ->text();
    input();
}

void Jisuanqi::OnClickedpb7() {
    digit = widget.pb7 ->text();
    input();
}

void Jisuanqi::OnClickedpb8() {
    digit = widget.pb8 ->text();
    input();
}

void Jisuanqi::OnClickedpb9() {
    digit = widget.pb9 ->text();
    input();
}

void Jisuanqi::OnClickedpbf0() {
    if (!dmaking) {
        rslt = "0.";
        dmaking = true;
    } else if (!rslt.contains(".")) {
        rslt += ".";
    }
}

void Jisuanqi::OnClickedpbf1() {
    calculate();
    yunsuanfu = ' ';
    d1 = 0;
    d2 = 0;
    d3 = 0;
    d1ready = false;
    dmaking = false;
}

void Jisuanqi::OnClickedpbf2() {
    calculate();
    d1 = rslt.toDouble();
    d1ready = true;
    dmaking = false;
    yunsuanfu = '+';
}

void Jisuanqi::OnClickedpbf3() {
    calculate();
    d1 = rslt.toDouble();
    d1ready = true;
    dmaking = false;
    yunsuanfu = '-';
}

void Jisuanqi::OnClickedpbf4() {
    calculate();
    d1 = rslt.toDouble();
    d1ready = true;
    dmaking = false;
    yunsuanfu = '*';
}

void Jisuanqi::OnClickedpbf5() {
    calculate();
    d1 = rslt.toDouble();
    d1ready = true;
    dmaking = false;
    yunsuanfu = '/';
}

void Jisuanqi::OnClickedpbf6() {
    if (dmaking) {
        if (rslt.startsWith("-")) {
            rslt.remove(0, 1);
        } else {
            if (rslt != "0" && rslt != "0.")
                rslt = "-" + rslt;
        }
    }
}

void Jisuanqi::OnClickedpbf7() {
    yunsuanfu = ' ';
    rslt = "0";
    digit = "";
    d1 = 0;
    d2 = 0;
    d3 = 0;
    dmaking = false;
    d1ready = false;
}

每次输入运算符时都应该执行一次计算(如果之前没有输入过运算符不会执行),并把结果存入d1以便下一次计算,再保存当前运算符,设置好输入状态;

输入等号除了计算,还应把显示结果以外的成员初始化

ui_jisuanqi.h

/********************************************************************************
** Form generated from reading UI file 'Jisuanqi.ui'
**
** Created by: Qt User Interface Compiler version 5.9.2
**
** 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 <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QToolBar>
#include <QtWidgets/QWidget>

#pragma execution_character_set("utf-8")  //解决编码问题

QT_BEGIN_NAMESPACE

class Ui_MainWindow
{
public:
    QWidget *centralwidget;
    QWidget *gridLayoutWidget;
    QGridLayout *gridLayout;
    QPushButton *pb3;
    QPushButton *pbf4;
    QPushButton *pb0;
    QPushButton *pbf2;
    QPushButton *pbf1;
    QPushButton *pbf5;
    QPushButton *pbf3;
    QPushButton *pb1;
    QPushButton *pb8;
    QPushButton *pb7;
    QPushButton *pb9;
    QPushButton *pb2;
    QPushButton *pbf0;
    QPushButton *pb4;
    QPushButton *pb5;
    QPushButton *pb6;
    QLineEdit *lineEdit;
    QWidget *horizontalLayoutWidget;
    QHBoxLayout *horizontalLayout;
    QPushButton *pbf7;
    QPushButton *pbf6;
    QMenuBar *menubar;
    QStatusBar *statusbar;
    QToolBar *toolBar;

    void setupUi(QMainWindow *MainWindow)
    {
        if (MainWindow->objectName().isEmpty())
            MainWindow->setObjectName(QStringLiteral("MainWindow"));
        MainWindow->resize(400, 450);
        MainWindow->setStyleSheet(QStringLiteral(""));
        centralwidget = new QWidget(MainWindow);
        centralwidget->setObjectName(QStringLiteral("centralwidget"));
        gridLayoutWidget = new QWidget(centralwidget);
        gridLayoutWidget->setObjectName(QStringLiteral("gridLayoutWidget"));
        gridLayoutWidget->setGeometry(QRect(10, 130, 381, 271));
        gridLayout = new QGridLayout(gridLayoutWidget);
        gridLayout->setObjectName(QStringLiteral("gridLayout"));
        gridLayout->setContentsMargins(0, 0, 0, 0);
        pb3 = new QPushButton(gridLayoutWidget);
        pb3->setObjectName(QStringLiteral("pb3"));

        gridLayout->addWidget(pb3, 6, 2, 1, 1);

        pbf4 = new QPushButton(gridLayoutWidget);
        pbf4->setObjectName(QStringLiteral("pbf4"));

        gridLayout->addWidget(pbf4, 5, 3, 1, 1);

        pb0 = new QPushButton(gridLayoutWidget);
        pb0->setObjectName(QStringLiteral("pb0"));

        gridLayout->addWidget(pb0, 7, 0, 1, 1);

        pbf2 = new QPushButton(gridLayoutWidget);
        pbf2->setObjectName(QStringLiteral("pbf2"));

        gridLayout->addWidget(pbf2, 7, 3, 1, 1);

        pbf1 = new QPushButton(gridLayoutWidget);
        pbf1->setObjectName(QStringLiteral("pbf1"));

        gridLayout->addWidget(pbf1, 7, 2, 1, 1);

        pbf5 = new QPushButton(gridLayoutWidget);
        pbf5->setObjectName(QStringLiteral("pbf5"));

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

        pbf3 = new QPushButton(gridLayoutWidget);
        pbf3->setObjectName(QStringLiteral("pbf3"));

        gridLayout->addWidget(pbf3, 6, 3, 1, 1);

        pb1 = new QPushButton(gridLayoutWidget);
        pb1->setObjectName(QStringLiteral("pb1"));

        gridLayout->addWidget(pb1, 6, 0, 1, 1);

        pb8 = new QPushButton(gridLayoutWidget);
        pb8->setObjectName(QStringLiteral("pb8"));

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

        pb7 = new QPushButton(gridLayoutWidget);
        pb7->setObjectName(QStringLiteral("pb7"));

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

        pb9 = new QPushButton(gridLayoutWidget);
        pb9->setObjectName(QStringLiteral("pb9"));

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

        pb2 = new QPushButton(gridLayoutWidget);
        pb2->setObjectName(QStringLiteral("pb2"));

        gridLayout->addWidget(pb2, 6, 1, 1, 1);

        pbf0 = new QPushButton(gridLayoutWidget);
        pbf0->setObjectName(QStringLiteral("pbf0"));

        gridLayout->addWidget(pbf0, 7, 1, 1, 1);

        pb4 = new QPushButton(gridLayoutWidget);
        pb4->setObjectName(QStringLiteral("pb4"));

        gridLayout->addWidget(pb4, 5, 0, 1, 1);

        pb5 = new QPushButton(gridLayoutWidget);
        pb5->setObjectName(QStringLiteral("pb5"));

        gridLayout->addWidget(pb5, 5, 1, 1, 1);

        pb6 = new QPushButton(gridLayoutWidget);
        pb6->setObjectName(QStringLiteral("pb6"));

        gridLayout->addWidget(pb6, 5, 2, 1, 1);

        lineEdit = new QLineEdit(centralwidget);
        lineEdit->setObjectName(QStringLiteral("lineEdit"));
        lineEdit->setGeometry(QRect(10, 10, 381, 51));
        lineEdit->setStyleSheet(QStringLiteral(""));
        horizontalLayoutWidget = new QWidget(centralwidget);
        horizontalLayoutWidget->setObjectName(QStringLiteral("horizontalLayoutWidget"));
        horizontalLayoutWidget->setGeometry(QRect(10, 80, 381, 51));
        horizontalLayout = new QHBoxLayout(horizontalLayoutWidget);
        horizontalLayout->setObjectName(QStringLiteral("horizontalLayout"));
        horizontalLayout->setContentsMargins(0, 0, 0, 0);
        pbf7 = new QPushButton(horizontalLayoutWidget);
        pbf7->setObjectName(QStringLiteral("pbf7"));

        horizontalLayout->addWidget(pbf7);

        pbf6 = new QPushButton(horizontalLayoutWidget);
        pbf6->setObjectName(QStringLiteral("pbf6"));

        horizontalLayout->addWidget(pbf6);

        MainWindow->setCentralWidget(centralwidget);
        menubar = new QMenuBar(MainWindow);
        menubar->setObjectName(QStringLiteral("menubar"));
        menubar->setGeometry(QRect(0, 0, 400, 28));
        MainWindow->setMenuBar(menubar);
        statusbar = new QStatusBar(MainWindow);
        statusbar->setObjectName(QStringLiteral("statusbar"));
        MainWindow->setStatusBar(statusbar);
        toolBar = new QToolBar(MainWindow);
        toolBar->setObjectName(QStringLiteral("toolBar"));
        MainWindow->addToolBar(Qt::TopToolBarArea, toolBar);

        retranslateUi(MainWindow);

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

    void retranslateUi(QMainWindow *MainWindow)
    {
        MainWindow->setWindowTitle(QApplication::translate("MainWindow", "计算器", Q_NULLPTR));
        pb3->setText(QApplication::translate("MainWindow", "3", Q_NULLPTR));
        pbf4->setText(QApplication::translate("MainWindow", "*", Q_NULLPTR));
        pb0->setText(QApplication::translate("MainWindow", "0", Q_NULLPTR));
        pbf2->setText(QApplication::translate("MainWindow", "+", Q_NULLPTR));
        pbf1->setText(QApplication::translate("MainWindow", "=", Q_NULLPTR));
        pbf5->setText(QApplication::translate("MainWindow", "/", Q_NULLPTR));
        pbf3->setText(QApplication::translate("MainWindow", "-", Q_NULLPTR));
        pb1->setText(QApplication::translate("MainWindow", "1", Q_NULLPTR));
        pb8->setText(QApplication::translate("MainWindow", "8", Q_NULLPTR));
        pb7->setText(QApplication::translate("MainWindow", "7", Q_NULLPTR));
        pb9->setText(QApplication::translate("MainWindow", "9", Q_NULLPTR));
        pb2->setText(QApplication::translate("MainWindow", "2", Q_NULLPTR));
        pbf0->setText(QApplication::translate("MainWindow", ".", Q_NULLPTR));
        pb4->setText(QApplication::translate("MainWindow", "4", Q_NULLPTR));
        pb5->setText(QApplication::translate("MainWindow", "5", Q_NULLPTR));
        pb6->setText(QApplication::translate("MainWindow", "6", Q_NULLPTR));
        pbf7->setText(QApplication::translate("MainWindow", "C", Q_NULLPTR));
        pbf6->setText(QApplication::translate("MainWindow", "+/-", Q_NULLPTR));
        toolBar->setWindowTitle(QApplication::translate("MainWindow", "toolBar", Q_NULLPTR));
    } // retranslateUi

};

namespace Ui {
    class MainWindow: public Ui_MainWindow {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_JISUANQI_H

main

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

int main(int argc, char *argv[]) {
    // initialize resources, if needed
    // Q_INIT_RESOURCE(resfile);

    QApplication app(argc, argv);
    Jisuanqi jsq;
    jsq.show();

    // create and show your widgets here

    return app.exec();
}

linux下   用netbeans+qt5.9制作

由于装了双系统,拷贝到Windows  VS2017+qt也能运行,窗口标题问号可能是Windows下Qt的字符编码问题,需要在ui.h中加上一行:#pragma execution_character_set("utf-8")  

如果要用生成的exe文件运行,需要在exe目录下放入相应的dll文件,在x:\QT\5.9.2\msvc2017_64文件夹下,对应64位,我的电脑没有装对应win32位的

 

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值