#include
Q_OBJECT public:
CWidget(QWidget *parent=0, char *name=0); ~CWidget(); private:
QLineEdit *edit; QPushButton *button[16]; QVBoxLayout *mainLayout; QHBoxLayout *topLayout; QGridLayout *bottomLayout; int firstNum; int secondNum; int oper; protected slots: void setValue(); void setOper(); void calculate(); void clear(); protected:
virtual bool event(QEvent *e); private: void initialize(); void createForm(); void onClicked(int key); }; #endif
cwidget.cpp: #include \#define KEY_CLR \#define KEY_ADD \#define KEY_SUB \#define KEY_MUL \#define KEY_DIV \
#define KEY_EQ \#define KEY_0 \#define KEY_1 \#define KEY_2 \#define KEY_3 \#define KEY_4 \#define KEY_5 \#define KEY_6 \#define KEY_7 \#define KEY_8 \#define KEY_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(\ }
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(\ 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->setAlignment(Qt::AlignRight); edit->setMaxLength(9); edit->setText(tr(\ 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
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(\ oper = key; }