netbeans+QT做的计算器

linux默认装了qt designer,没有creator,先做了ui再用netbeans构建项目完成

先上编译器自动生成的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>

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

jisuanqi.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();//信号与槽关联
    
public slots:
    void textChanged(); // 显示框槽函数
    
    void OnClickedpb0();//按键
    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;//运算结果
    int nYunsuanfu;//运算符个数0/1
    int nDots;//小数点个数0/1
    bool dmaking;//输入数字状态
    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";
    yunsuanfu = ' ';
    nYunsuanfu = 0;
    nDots = 0;
    dmaking = 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::textChanged() {
    widget.lineEdit->setText(rslt);
}

void Jisuanqi::OnClickedpb0() {
    if (rslt != "0" && dmaking)
        rslt += widget.pb0 ->text();
    else {
        rslt = widget.pb0 ->text();
        dmaking = true;
    }
}

void Jisuanqi::OnClickedpb1() {
    if (rslt != "0" && dmaking) {
        rslt += widget.pb1 ->text();
    } else {
        rslt = widget.pb1 ->text();
        dmaking = true;
    }
}

void Jisuanqi::OnClickedpb2() {
    if (rslt != "0" && dmaking)
        rslt += widget.pb2 ->text();
    else {
        rslt = widget.pb2 ->text();
        dmaking = true;
    }
}

void Jisuanqi::OnClickedpb3() {
    if (rslt != "0" && dmaking)
        rslt += widget.pb3 ->text();
    else {
        rslt = widget.pb3 ->text();
        dmaking = true;
    }
}

void Jisuanqi::OnClickedpb4() {
    if (rslt != "0" && dmaking)
        rslt += widget.pb4 ->text();
    else {
        rslt = widget.pb4 ->text();
        dmaking = true;
    }
}

void Jisuanqi::OnClickedpb5() {
    if (rslt != "0" && dmaking)
        rslt += widget.pb5 ->text();
    else {
        rslt = widget.pb5 ->text();
        dmaking = true;
    }
}

void Jisuanqi::OnClickedpb6() {
    if (rslt != "0" && dmaking)
        rslt += widget.pb6 ->text();
    else {
        rslt = widget.pb6 ->text();
        dmaking = true;
    }
}

void Jisuanqi::OnClickedpb7() {
    if (rslt != "0" && dmaking)
        rslt += widget.pb7 ->text();
    else {
        rslt = widget.pb7 ->text();
        dmaking = true;
    }
}

void Jisuanqi::OnClickedpb8() {
    if (rslt != "0" && dmaking)
        rslt += widget.pb8 ->text();
    else {
        rslt = widget.pb8 ->text();
        dmaking = true;
    }
}

void Jisuanqi::OnClickedpb9() {
    if (rslt != "0" && dmaking)
        rslt += widget.pb9 ->text();
    else {
        rslt = widget.pb9 ->text();
        dmaking = true;
    }
}

void Jisuanqi::OnClickedpbf0() {
    if (nDots == 0) {
        /*if (rslt == "0" && dmaking) {
            rslt = "0.";
            nDots = 1;
        }*/
        if (!dmaking) {
            rslt = "0.";
            dmaking = true;
            nDots = 1;
        } else {
            rslt += widget.pbf0 ->text();
            nDots = 1;
        }
    }
}

void Jisuanqi::OnClickedpbf1() {
    if (nYunsuanfu > 0 && dmaking) {
        d2 = rslt.toDouble();
        switch (yunsuanfu) {
            case '+':
                d3 = d1 + d2;
                break;
            case '-':
                d3 = d1 - d2;
                break;
            case '*':
                d3 = d1 * d2;
                break;
            case '/':
                if (d2 != 0) {
                    d3 = d1 / d2;
                } else {
                    d3 = 0;
                }
                break;
            default:
                break;
        }

        /*if (d3 == double(int(d3)))//这里是整数不用加小数点的方法,有了下面的方法可以去掉了
            rslt = QString::number(int(d3));
        else */
        {

            QString qst = QString::number((d3), 'f'); 
            
            //去掉小数末尾的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; //rslt = QString::number((d3), 'f'); //QString::number((d3), 'f',4)保留4位小数
        }
    }
    yunsuanfu = ' ';
    nYunsuanfu = 0;
    d1 = rslt.toDouble();
    d2 = 0;
    d3 = 0;
    dmaking = false;
    nDots = 0;
}

void Jisuanqi::OnClickedpbf2() {
    yunsuanfu = '+';
    nYunsuanfu = 1;
    d1 = rslt.toDouble();
    dmaking = false;
    nDots = 0;
}

void Jisuanqi::OnClickedpbf3() {
    yunsuanfu = '-';
    nYunsuanfu = 1;
    d1 = rslt.toDouble();
    dmaking = false;
    nDots = 0;
}

void Jisuanqi::OnClickedpbf4() {
    yunsuanfu = '*';
    nYunsuanfu = 1;
    d1 = rslt.toDouble();
    dmaking = false;
    nDots = 0;
}

void Jisuanqi::OnClickedpbf5() {
    yunsuanfu = '/';
    nYunsuanfu = 1;
    d1 = rslt.toDouble();
    dmaking = false;
    nDots = 0;
}

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";
    nYunsuanfu = 0;
    nDots = 0;
    d1 = 0;
    d2 = 0;
    d3 = 0;
    dmaking = false;
}

最后main

#include <QApplication>
//#include <QtCore/QCoreApplication>
//#include <iostream>
#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();
}

只实现了加减乘除,可全程显示结果,还可以优化

猜你喜欢

转载自blog.csdn.net/holyweng/article/details/81876522