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; }