?? 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"
#define KEY_1 "1"
#define KEY_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"
#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,
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()
{
delete edit;
delete *button;
delete mainLayout;
delete topLayout;
delete bottomLayout;
}
void CWidget::calculate()
{
switch(oper)
{
case Qt::Key_Plus:
firstNum += secondNum;break;
case Qt::Key_Minus:
firstNum -= secondNum;break;
case Qt::Key_Asterisk:
firstNum *= secondNum;break;
case Qt::Key_Slash:
firstNum /= secondNum;break;
default:
firstNum = firstNum;
}
edit->setText(QString::number(firstNum));
}
void CWidget::setValue()
{
QString tempStr;
tempStr = edit->text();
if(tempStr.length() < edit->maxLength())
tempStr += ((QPushButton *)sender())->text();
else
{
QMessageBox::information( this, tr("Calculator"),tr("too long!"));
}
if(-1==oper)
{
firstNum = tempStr.toInt();
edit->setText(QString::number(firstNum));
}
else
{
secondNum = tempStr.toInt();
edit->setText(QString::number(secondNum));
}
}
void CWidget::setOper()
{
QString str=((QPushButton *)sender())->text();
if(str == "+")
onClicked(Qt::Key_Plus);
else if(str == "-")
onClicked(Qt::Key_Minus);
else if(str == "*")
onClicked(Qt::Key_Asterisk);
else if(str == "/")
onClicked(Qt::Key_Slash);
else if(str == "=")
calculate();
}
void CWidget::clear()
{
edit->clear();
edit->setText(tr("%1").arg(0));
initialize();
}
void CWidget::initialize()
{
firstNum = 0;
secondNum = 0;
oper = -1;
}
void CWidget::createForm()
{
setMinimumSize(80,200);
setMaximumSize(80,200);
mainLayout = new QVBoxLayout(this,20);
topLayout = new QHBoxLayout(mainLayout,30);
edit = new QLineEdit(this,"edit");
edit->setAlignment(Qt::AlignRight);
edit->setMaxLength(9);
edit->setText(tr("%1").arg(0));
edit->setReadOnly(true);
topLayout->addWidget(edit);
bottomLayout = new QGridLayout(mainLayout,4,4,10);
int n;
for(int r=0; r<3; r++)
for(int c=0; c<3; c++)
{
n = c+3*r;
button[n] = new QPushButton(buttontext[n],this,buttontext[n]);
button[n]->setFixedSize(BUTTONWIDTH,BUTTONHEIGHT);
bottomLayout->addWidget(button[n],r,c);
connect(button[n],SIGNAL(clicked()),this,SLOT(setValue()));
}
button[9] = new QPushButton(buttontext[9],this,buttontext[9]);
button[9]->setFixedSize(BUTTONWIDTH,BUTTONHEIGHT);
bottomLayout->addWidget(button[9],0,3);
connect(button[9],SIGNAL(clicked()),this,SLOT(setOper()));
button[10] = new QPushButton(buttontext[10],this,buttontext[10]);
button[10]->setFixedSize(BUTTONWIDTH,BUTTONHEIGHT);
bottomLayout->addWidget(button[10],1,3);
connect(button[10],SIGNAL(clicked()),this,SLOT(setOper()));
button[11] = new QPushButton(buttontext[11],this,buttontext[11]);
button[11]->setFixedSize(BUTTONWIDTH,BUTTONHEIGHT);
bottomLayout->addWidget(button[11],2,3);
connect(button[11],SIGNAL(clicked()),this,SLOT(setOper()));
button[12] = new QPushButton(buttontext[12],this,buttontext[12]);
button[12]->setFixedSize(BUTTONWIDTH,BUTTONHEIGHT);
bottomLayout->addWidget(button[12],3,3);
connect(button[12],SIGNAL(clicked()),this,SLOT(setOper()));
button[13] = new QPushButton(buttontext[13],this,buttontext[13]);
button[13]->setFixedSize(BUTTONWIDTH,BUTTONHEIGHT);
bottomLayout->addWidget(button[13],3,2);
connect(button[13],SIGNAL(clicked()),this,SLOT(calculate()));
button[14] = new QPushButton(buttontext[14],this,buttontext[14]);
button[14]->setFixedSize(BUTTONWIDTH,BUTTONHEIGHT);
bottomLayout->addWidget(button[14],3,1);
connect(button[14],SIGNAL(clicked()),this,SLOT(clear()));
button[15] = new QPushButton(buttontext[15],this,buttontext[14]);
button[15]->setFixedSize(BUTTONWIDTH,BUTTONHEIGHT);
bottomLayout->addWidget(button[15],3,0);
connect(button[15],SIGNAL(clicked()),this,SLOT(setValue()));
}
bool CWidget::event(QEvent *e)
{
if(e->type() == QEvent::KeyPress)
{
QKeyEvent *KeyEvent = static_cast<QKeyEvent*>(e);
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;
case Qt::Key_Slash:
onClicked(Qt::Key_Slash);break;
case Qt::Key_Equal:
calculate();break;
}
}
return QWidget::event(e);
}
void CWidget::onClicked(int key)
{
edit->clear();
edit->setText(tr("%1").arg(0));
oper = key;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -