当前位置:文档之家› 简易计算器代码

简易计算器代码

connect(button[9],SIGNAL(clicked()),this,SLOT(setOper()));
button[10] = new QPushButton(buttontext[10],this,buttontext[10]); // “—”
button[10]->setFixedSize(BUTTONWIDTH,BUTTONHEIGHT);
edit->setText(tr("%1").arg(0)); //这个函数可获取文本编辑框中光标的位置,并显示在状态栏中
//setText(tr(“%1行%2列”).arg(rowNum).arg(colNum));
initialize(); //调用初始化函数
}
void CWidget::initialize()
cwidget.cpp
#include "cwidget.h"
#define KEY_CLR "CLR"
#define KEY_ADD "+"
#define KEY_SUB "-"
#define KEY_MUL "*"
#define KEY_DIV "/"
#define KEY_EQ "="
#define KEY_0 "0"
{
delete edit;
delete *button;
delete mainLayout;
delete topLayout;
delete bottomLayout;
}
void CWidget::calculate() //响应按键“=”,调用计算功能
{
switch(oper)
{
case Qt::Key_Plus: //加法
#define KEY_1 "1"
#define ቤተ መጻሕፍቲ ባይዱEY_2 "2"
#define KEY_3 "3"
#define KEY_4 "4"
#define KEY_5 "5"
#define KEY_6 "6"
#define KEY_7 "7"
#define KEY_8 "8"
#define KEY_9 "9"
if(str == "+")
onClicked(Qt::Key_Plus); //Qt中使用的“+”键的名称
else if(str == "-")
onClicked(Qt::Key_Minus); //Qt中使用的“—”键的名称。
else if(str == "*")
onClicked(Qt::Key_Asterisk); //Qt中使用的“*”键的名称。
edit->setMaxLength(9); //最多9位数
edit->setText(tr("%1").arg(0)); //光标位置
edit->setReadOnly(true); //设为只读模式
topLayout->addWidget(edit); //先布置显示框
bottomLayout = new QGridLayout(mainLayout,4,4,10); //布置在mainLayout里,4行、4列、间隔10
}
if(-1==oper) //没有按加减乘除、等于、清空键
{
firstNum = tempStr.toInt(); //把tempStr转换成整型数,赋给第一个计算数
edit->setText(QString::number(firstNum)); //变成字符串之后,显示
}
Else //之前按过加减乘除、等于、清空键
bottomLayout->addWidget(button[13],3,2); //第4行第3列
connect(button[13],SIGNAL(clicked()),this,SLOT(calculate())); //按下,调用calculate函数
button[14] = new QPushButton(buttontext[14],this,buttontext[14]); //清空
}
button[9] = new QPushButton(buttontext[9],this,buttontext[9]); // “ + ”
button[9]->setFixedSize(BUTTONWIDTH,BUTTONHEIGHT);
bottomLayout->addWidget(button[9],0,3); //第1行第4列
{
firstNum = 0;
secondNum = 0;
oper = -1;
}
void CWidget::createForm()
{
setMinimumSize(80,200); //最小窗口
setMaximumSize(80,200); //最大窗口
mainLayout = new QVBoxLayout(this,20); //整体布局,以当前窗口为基准,(垂直间隔)离边界20
#define BUTTONWIDTH 30 //定义按键的长度
#define BUTTONHEIGHT 30 //定义按键的宽度
static char *buttontext[] =//定义静态全局变量
{
KEY_1,KEY_2,KEY_3,KEY_4,
KEY_5,KEY_6,KEY_7,KEY_8,
{
secondNum = tempStr.toInt();
edit->setText(QString::number(secondNum));
}
}
void CWidget::setOper() //将按键转换成qt能识别的语言
{
QString str=((QPushButton *)sender())->text();
firstNum += secondNum;break;
case Qt::Key_Minus: //减法
firstNum -= secondNum;break;
case Qt::Key_Asterisk: //乘法
firstNum *= secondNum;break;
case Qt::Key_Slash: //除法
KEY_9,KEY_ADD,KEY_SUB,KEY_MUL,
KEY_DIV,KEY_EQ,KEY_CLR,KEY_0
};
CWidget::CWidget(QWidget *parent, char *name) //构造函数
{
initialize();
createForm();
}
CWidget::~CWidget() //析构函数,销毁各个按键
button[n]->setFixedSize(BUTTONWIDTH,BUTTONHEIGHT); //设置按键大小
bottomLayout->addWidget(button[n],r,c); //从右上角为第1(r+1)行,第1(c+1)列
connect(button[n],SIGNAL(clicked()),this,SLOT(setValue()));
connect(button[12],SIGNAL(clicked()),this,SLOT(setOper()));
button[13] = new QPushButton(buttontext[13],this,buttontext[13]); // “ = ”
button[13]->setFixedSize(BUTTONWIDTH,BUTTONHEIGHT);
int n;
for(int r=0; r<3; r++) //1~9阵列
for(int c=0; c<3; c++)
{
n = c+3*r;
button[n] = new QPushButton(buttontext[n],this,buttontext[n]); //构造一个名称为n、父对象为parent并且文本为n的推动按钮
else if(str == "/")
onClicked(Qt::Key_Slash); //Qt中使用的“/”键的名称。
else if(str == "=")
onClicked(Qt:: Key_Equal); //点击等于号,调用计算函数
}
void CWidget::clear()
{
edit->clear(); //清空显示框,clear()为内部函数
switch(KeyEvent->key())
{
case Qt::Key_Plus:
onClicked(Qt::Key_Plus);break;
case Qt::Key_Minus:
onClicked(Qt::Key_Minus);break;
case Qt::Key_Asterisk:
onClicked(Qt::Key_Asterisk);break;
if(tempStr.length() < edit->maxLength()) //小于显示框允许的最大位数
tempStr += ((QPushButton *)sender())->text(); //将按键对应的数值添加tempStr
相关主题