Qt之简易计算器

时间:2022-02-17 01:16:19

写之原因:

本人是一名菜鸟,学了Qt也就十天左右吧。

学习一种对我来说比较新的语言,最好的学习方式就是去真刀真枪的干一场!所以,闲来无事,就打算写一个小应用。

思来想去,觉得还是写一个简单的计算器吧,因为其他的涉及的还不是特别深,感觉自己能驾驭的就是计算器这种,简单而实用的小应用。

代码:

(1)calculate.h文件:

#ifndef CALCULATE_H
#define CALCULATE_H

#include <QWidget>

class QPushButton;
class QLineEdit;
class QHBoxLayout;
class QVBoxLayout;
class QChar;

class calculate : public QWidget
{
Q_OBJECT

public:
calculate(QWidget *parent = 0);
private slots:
void slotButton1();
void slotButton2();
void slotButton3();
void slotButton4();
void slotButton5();
void slotButton6();
void slotButton7();
void slotButton8();
void slotButton9();
void slotButton0();
void Plus();
void Minus();
void Multiply();
void Divide();
void Clear();
void Equal();
signals:
void PlusSignal(char state);

private:
void NewButton();
void NewLayout();
//virtual function
void keyPressEvent(QKeyEvent *key);

char state;

long long num1,num2,num3,num4,num5,num6,num7,
num8,num9,num0;
double a,b;

//Number Button
QPushButton *button1;
QPushButton *button2;
QPushButton *button3;
QPushButton *button4;
QPushButton *button5;
QPushButton *button6;
QPushButton *button7;
QPushButton *button8;
QPushButton *button9;
QPushButton *button0;

//Operator Button
QPushButton *clear;
QPushButton *buttonPlus;
QPushButton *buttonMinus;
QPushButton *buttonMultiply;
QPushButton *buttonDivide;
QPushButton *buttonEqual;

//TextLine
QLineEdit *textline;

//Layout
QHBoxLayout *Hlayout0;
QHBoxLayout *Hlayout1;
QHBoxLayout *Hlayout2;
QHBoxLayout *Hlayout3;
QHBoxLayout *Hlayout4;
QHBoxLayout *Hlayout5;
QHBoxLayout *Hlayout6;

QVBoxLayout *Vlayout;
QVBoxLayout *Vlayout2;

};

#endif // CALCULATE_H
(2)calculate.cpp文件

#include "calculate.h"
#include "QPushButton"
#include "QLineEdit"
#include "QHBoxLayout"
#include "QVBoxLayout"
#include "QMessageBox"
#include "QKeyEvent"

calculate::calculate(QWidget *parent)
: QWidget(parent)
{
//initial
a = 0;
b = 0;
state = 'N';
//textline
textline = new QLineEdit;
textline->setFixedHeight(80);
textline->setStyleSheet("font-size:32px");
textline->setText(tr("By Wsy..."));
textline->setAlignment(Qt::AlignRight | Qt::AlignBottom);

//button
NewButton();

//Layout
NewLayout();

//setWindow
this->setWindowTitle(tr("Calculate"));
this->resize(300,400);

}

//NewButton
void calculate::NewButton()
{
button1 = new QPushButton(tr("1"));
button1->setFixedHeight(40);
connect(button1,SIGNAL(clicked()),this,SLOT(slotButton1()));

button2 = new QPushButton(tr("2"));
button2->setFixedHeight(40);
connect(button2,SIGNAL(clicked()),this,SLOT(slotButton2()));

button3 = new QPushButton(tr("3"));
button3->setFixedHeight(40);
connect(button3,SIGNAL(clicked()),this,SLOT(slotButton3()));

button4 = new QPushButton(tr("4"));
button4->setFixedHeight(40);
connect(button4,SIGNAL(clicked()),this,SLOT(slotButton4()));

button5 = new QPushButton(tr("5"));
button5->setFixedHeight(40);
connect(button5,SIGNAL(clicked()),this,SLOT(slotButton5()));

button6 = new QPushButton(tr("6"));
button6->setFixedHeight(40);
connect(button6,SIGNAL(clicked()),this,SLOT(slotButton6()));


button7 = new QPushButton(tr("7"));
button7->setFixedHeight(40);
connect(button7,SIGNAL(clicked()),this,SLOT(slotButton7()));

button8 = new QPushButton(tr("8"));
button8->setFixedHeight(40);
connect(button8,SIGNAL(clicked()),this,SLOT(slotButton8()));

button9 = new QPushButton(tr("9"));
button9->setFixedHeight(40);
connect(button9,SIGNAL(clicked()),this,SLOT(slotButton9()));

button0 = new QPushButton(tr("0"));
button0->setFixedHeight(40);
connect(button0,SIGNAL(clicked()),this,SLOT(slotButton0()));

clear = new QPushButton(tr("clear"));
clear->setFixedHeight(40);
connect(clear,SIGNAL(clicked()),this,SLOT(Clear()));

buttonPlus = new QPushButton(tr("+"));
buttonPlus->setFixedHeight(40);
connect(buttonPlus,SIGNAL(clicked()),this,SLOT(Plus()));

buttonMinus = new QPushButton(tr("-"));
buttonMinus->setFixedHeight(40);
connect(buttonMinus,SIGNAL(clicked()),this,SLOT(Minus()));

buttonMultiply = new QPushButton(tr("*"));
buttonMultiply->setFixedHeight(40);
connect(buttonMultiply,SIGNAL(clicked()),this,SLOT(Multiply()));

buttonDivide = new QPushButton(tr("/"));
buttonDivide->setFixedHeight(40);
connect(buttonDivide,SIGNAL(clicked()),this,SLOT(Divide()));

buttonEqual = new QPushButton(tr("="));
buttonEqual->setFixedHeight(230);
connect(buttonEqual,SIGNAL(clicked()),this,SLOT(Equal()));
}

//keypressEvent
void calculate::keyPressEvent(QKeyEvent *key)
{
switch(key->key())
{
case Qt::Key_0:
emit button0->click();
break;
case Qt::Key_1:
emit button1->click();
break;
case Qt::Key_2:
emit button2->click();
break;
case Qt::Key_3:
emit button3->click();
break;
case Qt::Key_4:
emit button4->click();
break;
case Qt::Key_5:
emit button5->click();
break;
case Qt::Key_6:
emit button6->click();
break;
case Qt::Key_7:
emit button7->click();
break;
case Qt::Key_8:
emit button8->click();
break;
case Qt::Key_9:
emit button9->click();
case Qt::Key_Plus:
emit buttonPlus->click();
case Qt::Key_Minus:
emit buttonMinus->click();
case Qt::Key_multiply:
emit buttonMultiply->click();
case Qt::Key_division:
emit buttonDivide->click();
case Qt::Key_Enter:
emit buttonEqual->click();
break;
}
}

//NewLayout
void calculate::NewLayout()
{
Hlayout1 = new QHBoxLayout;
Hlayout1->addWidget(button1);
Hlayout1->addWidget(button2);
Hlayout1->addWidget(clear);
Hlayout1->setSpacing(10);

Hlayout2 = new QHBoxLayout;
Hlayout2->addWidget(button3);
Hlayout2->addWidget(button4);
Hlayout2->addWidget(buttonPlus);
Hlayout2->setSpacing(10);

Hlayout3 = new QHBoxLayout;
Hlayout3->addWidget(button5);
Hlayout3->addWidget(button6);
Hlayout3->addWidget(buttonMinus);
Hlayout3->setSpacing(10);

Hlayout4 = new QHBoxLayout;
Hlayout4->addWidget(button7);
Hlayout4->addWidget(button8);
Hlayout4->addWidget(buttonMultiply);
Hlayout4->setSpacing(10);

Hlayout5 = new QHBoxLayout;
Hlayout5->addWidget(button9);
Hlayout5->addWidget(button0);
Hlayout5->addWidget(buttonDivide);
Hlayout5->setSpacing(10);


Vlayout = new QVBoxLayout;
Vlayout->addLayout(Hlayout1);
Vlayout->addLayout(Hlayout2);
Vlayout->addLayout(Hlayout3);
Vlayout->addLayout(Hlayout4);
Vlayout->addLayout(Hlayout5);

Hlayout6 = new QHBoxLayout;
Hlayout6->addLayout(Vlayout);
Hlayout6->addWidget(buttonEqual);

Vlayout2 = new QVBoxLayout;
Vlayout2->addWidget(textline);
Vlayout2->addLayout(Hlayout6);

setLayout(Vlayout2);
}

//SLOT_Button
void calculate::slotButton1()
{
num1 = 1;

if( a < 999999999999999 )
{
a = a*10 + num1;

QString buttonStr1;
buttonStr1=QString::number(a);

textline->clear();
textline->setText(buttonStr1);
}
else
QMessageBox::information(NULL,"info","Out Range...",QMessageBox::Ok);

}

void calculate::slotButton2()
{
num2 = 2;

if( a < 999999999999999 )
{
a = a*10 + num2;

QString buttonStr2;
buttonStr2=QString::number(a);

textline->clear();
textline->setText(buttonStr2);
}
else
QMessageBox::information(NULL,"info","Out Range...",QMessageBox::Ok);

}

void calculate::slotButton3()
{
num3 = 3;

if(a < 999999999999999 )
{
a = a*10 + num3;

QString buttonStr3;
buttonStr3=QString::number(a);

textline->clear();
textline->setText(buttonStr3);
}
else
QMessageBox::information(NULL,"info","Out Range...",QMessageBox::Ok);

}

void calculate::slotButton4()
{
num4 = 4;

if(a < 999999999999999 )
{
a = a*10 + num4;

QString buttonStr4;
buttonStr4=QString::number(a);

textline->clear();
textline->setText(buttonStr4);
}
else
QMessageBox::information(NULL,"info","Out Range...",QMessageBox::Ok);

}

void calculate::slotButton5()
{
num5 = 5;

if(a < 999999999999999 )
{
a = a*10 + num5;

QString buttonStr5;
buttonStr5=QString::number(a);

textline->clear();
textline->setText(buttonStr5);
}
else
QMessageBox::information(NULL,"info","Out Range...",QMessageBox::Ok);

}

void calculate::slotButton6()
{
num6 = 6;

if(a < 999999999999999 )
{
a = a*10 + num6;

QString buttonStr6;
buttonStr6=QString::number(a);

textline->clear();
textline->setText(buttonStr6);
}
else
QMessageBox::information(NULL,"info","Out Range...",QMessageBox::Ok);

}

void calculate::slotButton7()
{
num7 = 7;

if(a < 999999999999999 )
{
a = a*10 + num7;

QString buttonStr7;
buttonStr7=QString::number(a);

textline->clear();
textline->setText(buttonStr7);
}
else
QMessageBox::information(NULL,"info","Out Range...",QMessageBox::Ok);

}

void calculate::slotButton8()
{
num8 = 8;

if(a < 999999999999999 )
{
a = a*10 + num8;

QString buttonStr8;
buttonStr8=QString::number(a);

textline->clear();
textline->setText(buttonStr8);
}
else
QMessageBox::information(NULL,"info","Out Range...",QMessageBox::Ok);

}

void calculate::slotButton9()
{
num9 = 9;

if(a < 999999999999999 )
{
a = a*10 + num9;

QString buttonStr9;
buttonStr9=QString::number(a);

textline->clear();
textline->setText(buttonStr9);
}
else
QMessageBox::information(NULL,"info","Out Range...",QMessageBox::Ok);

}
void calculate::slotButton0()
{
num0 = 0;

if(a < 999999999999999 )
{
a = a*10 + num0;

QString buttonStr0;
buttonStr0=QString::number(a);

textline->clear();
textline->setText(buttonStr0);
}
else
QMessageBox::information(NULL,"info","Out Range...",QMessageBox::Ok);

}

//SLOT_Operator
void calculate::Plus()
{
if(a!=0)
{
this->state = '+';
b = a;
a = 0;
textline->clear();
}
else
{
a=0;
b = a;
a = 0;
this->state = '+';
textline->clear();
}
}

void calculate::Minus()
{
b = a ;
a = 0 ;
this->state = '-';
textline->clear();
}

void calculate::Multiply()
{
b = a ;
a = 0 ;
this->state = '*';
textline->clear();

}

void calculate::Divide()
{
b = a ;
a = 0 ;
this->state = '/';
textline->clear();
}

void calculate::Clear()
{
a = 0;
b = 0;
textline->setText(QString::number(0));
}

void calculate::Equal()
{
if(state == '+')
{
a = a + b;
textline->clear();
textline->setText(QString::number(a));
state = 'N';
}
else if(state == '-')
{
a = b - a ;
textline->clear();
textline->setText(QString::number(a));
state = 'N';
}
else if(state == '*')
{
a = b * a ;
textline->clear();
textline->setText(QString::number(a));
state = 'N';
}
else if(state == '/')
{
if( b == 0 )
{
QMessageBox::information(NULL,"Error","The first number is 0",QMessageBox::Ok);
this->Clear();
}
else
{
a = b / a;
textline->clear();
textline->setText(QString::number(a));
state = 'N';
}

}
}
由于为了可以实现浮点数除法,所以我将两个变量设为double类型。

上述代码还利用了虚函数重构,将keyPressEvent()函数重新写了,目的是为了可以实现键盘输入数字然后进行运算,但是目前对于运算符号+、-、×、/还没有完善。

如果有朋友有什么好的想法,真心求指教。

以下是计算器的截图:

Qt之简易计算器

Qt之简易计算器

有点丑 = =||| 请见谅....