关闭

qt写的一个计算器程序

966人阅读 评论(0) 收藏 举报
分类:

界面如下

上面的图片是通过设计界面实现的,水平太菜不会全用代码实现程序在我的qt5.3和qt5.6上都可以正常运行,源代码链接http://download.csdn.net/detail/qq_28637193/9618481

下面是头文件mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QDialog>
#include<QComboBox>
namespace Ui {
class MainWindow;
}
enum ActionFlag
{
    NoAction,  //没有运算
    additonFlag,//加
    Subtraction ,//减
    MultiplicationFlag,//乘
    DivisonFlag,//除
};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
  void displayNumber();
  void beep();
private:
    Ui::MainWindow *ui;
    ActionFlag currentAction;//当前活动
    QString lastNumber;
    QString currentNumber;
    bool isEmpty;  //
    QDialog *selectDialog; //新的窗口
    QComboBox *selectCombo;


public slots:
void clear();   //清空
void actionChanged();  //加减乘除活动
void inputNumber();   //输入数据
void negationClicked();  //正负转换
void percentageClicked();   //百分比
void decimalPointClicked();
void evaluationClicked();  //等号
void aboutApp();
void unitSelect();
void unitClac();

};

#endif // MAINWINDOW_H


下面是mainwindow.cpp代码

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"QDebug"
#include"QMessageBox"
#include<QDialogButtonBox>
#include<QGroupBox>
#include<QtAlgorithms>
#include<qmath.h>
#include<QDialog>
#include<QIcon>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    currentAction(NoAction),
    isEmpty(true)
{
    ui->setupUi(this);
    //customizeWinsowHint 关闭默认窗口标题提示。
    setWindowFlags(Qt::CustomizeWindowHint|Qt::WindowCloseButtonHint|Qt::WindowMinimizeButtonHint);
    //清除信号
    connect(ui->pushB_clear,SIGNAL(clicked(bool)),this,SLOT(clear()));
    //正负号转换
    connect(ui->puB_negetion,SIGNAL(clicked(bool)),this,SLOT(negationClicked()));
          //百分号取余数
          connect(ui->pushB_percentage,SIGNAL(clicked(bool)),this,SLOT(percentageClicked()));
    //小数点
    connect(ui->puB_comma,SIGNAL(clicked()),this,SLOT(decimalPointClicked()));
            //等于号
            connect(ui->puB_equal,SIGNAL(clicked(bool)),this,SLOT(evaluationClicked()));
    //加
    connect(ui->puB_add,SIGNAL(clicked(bool)),this,SLOT(actionChanged()));
            //减
            connect(ui->puB_subtract,SIGNAL(clicked(bool)),this,SLOT(actionChanged()));
    //乘
    connect(ui->puB_multipty,SIGNAL(clicked(bool)),this,SLOT(actionChanged()));
    //除
    connect(ui->puB_division,SIGNAL(clicked(bool)),this,SLOT(actionChanged()));
    //0-9
    connect(ui->puB_0,SIGNAL(clicked(bool)),this,SLOT(inputNumber()));
        connect(ui->puB_1,SIGNAL(clicked(bool)),this,SLOT(inputNumber()));
        connect(ui->puB_2,SIGNAL(clicked(bool)),this,SLOT(inputNumber()));
        connect(ui->puB_3,SIGNAL(clicked(bool)),this,SLOT(inputNumber()));
        connect(ui->puB_4,SIGNAL(clicked(bool)),this,SLOT(inputNumber()));
        connect(ui->puB_5,SIGNAL(clicked(bool)),this,SLOT(inputNumber()));
        connect(ui->puB_6,SIGNAL(clicked(bool)),this,SLOT(inputNumber()));
        connect(ui->puB_7,SIGNAL(clicked(bool)),this,SLOT(inputNumber()));
        connect(ui->puB_8,SIGNAL(clicked(bool)),this,SLOT(inputNumber()));
        connect(ui->puB_9,SIGNAL(clicked(bool)),this,SLOT(inputNumber()));

        connect(ui->action_qt,SIGNAL(triggered(bool)),qApp,SLOT(aboutQt()));
        connect(ui->actionauther,SIGNAL(triggererd(bool)),this,SLOT(aboutApp()));
        connect(ui->action_zhuanhuan,SIGNAL(triggered(bool)),this,SLOT(unitSelect()));

}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::clear()
{
    if(ui->pushB_clear->text()=="AC")
        {
            currentAction=NoAction;
            currentNumber='0';
            lastNumber='0';
            isEmpty=true;
            ui->pushB_clear->setToolTip("clear");


        }
        else if(ui->pushB_clear->text()=="C")
        {
            currentNumber='0';
            isEmpty=true;
            ui->pushB_clear->setText("AC");
            ui->pushB_clear->setToolTip("clear");
       displayNumber();
        }

}
void MainWindow::negationClicked()
{
    if(!isEmpty||(currentNumber.size()==0))
        {
            currentNumber="0";
            isEmpty=true;
        }
        else
        {
            if(currentNumber.at(0)=='-')
            {
                //remove删除指定位置指定长度
                currentNumber.remove(0,1);
            }
            else
            {
                currentNumber.push_front("-");
            }
            displayNumber();
            statusBar()->showMessage("符号转换",3000);
        }

}

void MainWindow::percentageClicked()
{
    if(!isEmpty)
       {
           currentNumber="0.";
           isEmpty=true;
       }
       //indexof可以查找前面是否存在指定符号
       else if(currentNumber.indexOf(".")==-1)
       {
           currentNumber.push_back(".");
       }
       displayNumber();
       statusBar()->showMessage("输入了小数点",3000);

}
void MainWindow::decimalPointClicked()  //小数
{
    if(!isEmpty)
    {
        currentNumber="0.";
        isEmpty=true;
    }
    //indexof可以查找前面是否存在指定符号
    else if(currentNumber.indexOf(".")==-1)
    {
        currentNumber.push_back(".");
    }
    displayNumber();
    statusBar()->showMessage("输入了小数点",3000);
}
void MainWindow::evaluationClicked()
{

    QString jieguo;
        switch(currentAction){
        case NoAction:
        {
            statusBar()->showMessage("请选择一个运算",3000);
            return;
            break;
        }
           case additonFlag:
        {     //qDebug()<<lastNumber.toDouble()+currentNumber.toDouble()<<endl;
            currentNumber=QString::number(lastNumber.toDouble()+currentNumber.toDouble(),'g',6);
            qDebug()<<currentNumber<<endl;

            statusBar()->showMessage("计算完成",2000);
            break;
            }
        case Subtraction:
        {
            currentNumber=QString::number(lastNumber.toDouble()-currentNumber.toDouble(),'g',6);
            statusBar()->showMessage("计算完成",2000);
            break;
        }
        case MultiplicationFlag:
        {
            currentNumber=QString::number(lastNumber.toDouble()*currentNumber.toDouble(),'g',6);
            statusBar()->showMessage("计算完成",2000);
            break;
        }
        case DivisonFlag:
        {
            currentNumber=QString::number(lastNumber.toDouble()/currentNumber.toDouble(),'g',6);
            statusBar()->showMessage("计算完成",2000);
            break;
        }

        default:
            break;
        }
        currentAction=NoAction;
       ui->label->setText(currentNumber);
        statusBar()->showMessage("计算成功",3000);
    }
    void MainWindow::actionChanged()
    {
        //获取源对象,不存在对象时返回0
        QPushButton *currentButton=qobject_cast<QPushButton*>(this->sender());
        if(currentButton)
        {
            isEmpty=false;
            if(currentButton==ui->puB_add)
            {
                currentAction=additonFlag;
                //返回状态栏信息,如果状态兰不存在
                statusBar()->showMessage("输入了加号",3000);
            }
            else if(currentButton==ui->puB_subtract)
            { isEmpty=false;
                currentAction=Subtraction;
                statusBar()->showMessage("输入了减号",3000);
            }
            else if(currentButton==ui->puB_multipty)
            {
                currentAction=MultiplicationFlag;
                statusBar()->showMessage("输入了乘号",3000);
            }
            else if(currentButton==ui->puB_division)
            {
                currentAction=DivisonFlag;
                statusBar()->showMessage("输入了除号",3000);
            }
            else
            {
                currentAction=NoAction;
            }
        }
}
    void MainWindow::inputNumber()
    {
        QPushButton *currentButton=qobject_cast<QPushButton*>(this->sender());
        if(currentButton)
        {
            int intput;
            if(currentButton==ui->puB_0)
                intput=0;
            if(currentButton==ui->puB_1)
                intput=1;

            if(currentButton==ui->puB_2)
                intput=2;

            if(currentButton==ui->puB_3)
                intput=3;

            if(currentButton==ui->puB_4)
                intput=4;

            if(currentButton==ui->puB_5)
                intput=5;

            if(currentButton==ui->puB_6)
                intput=6;
            if(currentButton==ui->puB_7)
                intput=7;

            if(currentButton==ui->puB_8)
                intput=8;

            if(currentButton==ui->puB_9)
                intput=9;

            ui->pushB_clear->setText("C");
            //如果当前不是空的,则清空显示的地方
            if(!isEmpty)
            {
                lastNumber=currentNumber;
                currentNumber.clear();
                isEmpty=true;

            }
            if(currentNumber=="0")
            {
                currentNumber.clear();
            }
            switch(intput)
            {
             case 0:
            {
                currentNumber.append("0");
                beep();
                statusBar()->showMessage("输入了0",3000);
                break;
            }
            case 1:
            {
                currentNumber.append("1");
                beep();
                statusBar()->showMessage("输入了1" ,3000);
                break;
            }
             case 2:
            {
                currentNumber.append("2");
                statusBar()->showMessage("输入了2",3000);
                break;
            }
            case 3:
            {
                currentNumber.append("3");
                statusBar()->showMessage("输入了4",3000);
                break;
            }
            case 4:
            {

                currentNumber.append("4");
                statusBar()->showMessage("输入了4",3000);
                break;
            }
            case 5:
            {
                currentNumber.append("5");
                statusBar()->showMessage("输入了5",3000);
                break;
            }
            case 6:
            {
                currentNumber.append("6");
                statusBar()->showMessage("输入了6",3000);
                break;
            }
            case 7:
            {
                currentNumber.append("7");
                statusBar()->showMessage("输入了7",3000);
                break;
            }

            case 8:
            {
                currentNumber.append("8");
                statusBar()->showMessage("输入了8",3000);
                break;
            }
            case 9:
            {
                currentNumber.append("9");
                statusBar()->showMessage("输入了9",3000);
                break;
            }

            default:
                break;
            }

        }
       displayNumber();
    }
    void MainWindow::displayNumber()
    {
        ui->label->setText(currentNumber);
    }
    void MainWindow::aboutApp()
    {
        QMessageBox::about(this,"关于计算器","此程序开发者车");

    }
    void MainWindow::unitSelect()
    {

        selectDialog=new QDialog(this);
        QDialogButtonBox *buttonBox;
        QGroupBox *groupBox;
        QGroupBox *groupBox_2;
        QLabel *displayLabel;

        selectDialog->resize(400,150);
        buttonBox=new QDialogButtonBox(selectDialog);
        buttonBox->setGeometry(30,100,341,32);
         qDebug()<<5<<endl;
        buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);

        groupBox =new QGroupBox(selectDialog);
        groupBox_2=new QGroupBox(selectDialog);
        groupBox->setGeometry(10,10,180,80);
        groupBox_2->setGeometry(210,10,180,80);
        groupBox->setTitle("要转换的数值");
        groupBox_2->setTitle("转换后的单位");
        displayLabel= new QLabel(ui->label->text(),selectDialog);  //显示要转换的数
        displayLabel->setGeometry(10,40,20,26);
        selectCombo=new QComboBox(groupBox_2);
        selectCombo->setGeometry(10,40,120,26);
        selectCombo->insertItem(0,"米->厘米");
        selectCombo->insertItem(1,"米->平方米");
        selectDialog->show();   //显示对话框
        connect(buttonBox,SIGNAL(rejected()),selectDialog,SLOT(deleteLater()));
        connect(buttonBox,SIGNAL(accepted()),this,SLOT(unitClac()));

        qDebug()<<7<<endl;
    }
    void MainWindow::unitClac()
    {
        switch(selectCombo->currentIndex())
        {
        case 0:
            currentNumber=QString::number(currentNumber.toDouble()*100);
            break;
        case 1:
            currentNumber=QString::number(pow(currentNumber.toDouble(),2));
            break;
        default:
            break;
        }
        selectDialog->deleteLater();
            displayNumber();

    }
    void MainWindow::beep()
    {
       QApplication::beep ();
    }
下面是main.cpp

#include "mainwindow.h"
#include <QApplication>
#include<QIcon>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    MainWindow w;
    w.show();

    return a.exec();
}

实现程序的图标我这里用的是rc的方法

RC_FILE += logo.rc
简单的分析就是把图片位置写到rc文件中然后加载rc文件,这个网上的教程很多大家可以搜素下


1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:35793次
    • 积分:619
    • 等级:
    • 排名:千里之外
    • 原创:25篇
    • 转载:5篇
    • 译文:0篇
    • 评论:22条
    文章分类
    最新评论