Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "calculater.h"
- #include<QApplication>
- #include<QKeyEvent>
- Calculater::Calculater(QWidget *parent) :
- QMainWindow(parent)
- {
- QFont style1("Calibri Light", 12, QFont::Normal);//left section
- QFont style2("Calibri Light", 18, QFont::Normal);//right section
- QFont style3("Arial", 18, QFont::Normal);//label
- QFont style4("Arial", 18, QFont::Bold); //number key
- QFont style5("Arial", 12, QFont::Normal);
- label=new QLabel("0",this);
- label->setGeometry(QRect(QPoint(10, 10), QSize(580, 315)));
- label->setTextInteractionFlags(Qt::TextEditorInteraction);//let user select the text and revise it
- label->setFont(style3);
- label->setAlignment(Qt::AlignTop);
- label->setAlignment(Qt::AlignRight);
- print_button=new QPushButton("Print",this) ;
- print_button->setGeometry(QRect(QPoint(0,325),QSize(150,55)));
- print_button->setFont(style1);
- connect(print_button,SIGNAL(released()),this,SLOT(print()));
- setInteger_button=new QPushButton("Set Integer",this) ;
- setInteger_button->setGeometry(QRect(QPoint(0,380),QSize(150,55)));
- setInteger_button->setFont(style1);
- connect(setInteger_button,SIGNAL(released()),this,SLOT(setInteger()));
- setDecimal_button=new QPushButton("Set Decimal",this);
- setDecimal_button->setGeometry(QRect(QPoint(0,435),QSize(150,55)));
- setDecimal_button->setFont(style1);
- connect(setDecimal_button,SIGNAL(released()),this,SLOT(setDecimal()));
- setComplex_button=new QPushButton("Set Complex",this);
- setComplex_button->setGeometry(QRect(QPoint(0,490),QSize(150,55)));
- setComplex_button->setFont(style1);
- connect(setComplex_button,SIGNAL(released()),this,SLOT(setComplex()));
- submit_button=new QPushButton("Submit",this);
- submit_button->setGeometry(QRect(QPoint(0,545),QSize(150,55)));
- submit_button->setFont(style1);
- connect(submit_button,SIGNAL(released()),this,SLOT(submit()));
- power_button=new QPushButton("power(a,b)",this);
- power_button->setGeometry(QRect(QPoint(0,600),QSize(150,55)));
- power_button->setFont(style1);
- connect(power_button,SIGNAL(released()),this,SLOT(power()));
- factorial_button=new QPushButton("N!",this);
- factorial_button->setGeometry(QRect(QPoint(0,655),QSize(150,55)));
- factorial_button->setFont(style1);
- connect(factorial_button,SIGNAL(released()),this,SLOT(factorial()));
- add_button=new QPushButton("+",this);
- add_button->setGeometry(QRect(QPoint(160,325),QSize(110,77)));
- add_button->setFont(style2);
- connect(add_button,SIGNAL(released()),this,SLOT(add()));
- min_button=new QPushButton("-",this);
- min_button->setGeometry(QRect(QPoint(160,402),QSize(110,77)));
- min_button->setFont(style2);
- connect(min_button,SIGNAL(released()),this,SLOT(min()));
- mul_button=new QPushButton("*",this);
- mul_button->setGeometry(QRect(QPoint(160,479),QSize(110,77)));
- mul_button->setFont(style2);
- connect(mul_button,SIGNAL(released()),this,SLOT(mul()));
- div_button=new QPushButton("/",this);
- div_button->setGeometry(QRect(QPoint(160,556),QSize(110,77)));
- div_button->setFont(style2);
- connect(div_button,SIGNAL(released()),this,SLOT(div()));
- leftBucket_button=new QPushButton("(",this);
- leftBucket_button->setGeometry(QRect(QPoint(160,633),QSize(110,77)));
- leftBucket_button->setFont(style2);
- connect(leftBucket_button,SIGNAL(released()),this,SLOT(leftBucket()));
- deleteAll_button=new QPushButton("CE",this);
- deleteAll_button->setGeometry(QRect(QPoint(270,325),QSize(110,77)));
- deleteAll_button->setFont(style2);
- connect(deleteAll_button,SIGNAL(released()),this,SLOT(deleteAll()));
- seven_button=new QPushButton("7",this);
- seven_button->setGeometry(QRect(QPoint(270,402),QSize(110,77)));
- seven_button->setFont(style4);
- connect(seven_button,SIGNAL(released()),this,SLOT(seven()));
- four_button=new QPushButton("4",this);
- four_button->setGeometry(QRect(QPoint(270,479),QSize(110,77)));
- four_button->setFont(style4);
- connect(four_button,SIGNAL(released()),this,SLOT(four()));
- one_button=new QPushButton("1",this);
- one_button->setGeometry(QRect(QPoint(270,556),QSize(110,77)));
- one_button->setFont(style4);
- connect(one_button,SIGNAL(released()),this,SLOT(one()));
- rightBucket_button=new QPushButton(")",this);
- rightBucket_button->setGeometry(QRect(QPoint(270,633),QSize(110,77)));
- rightBucket_button->setFont(style2);
- connect(rightBucket_button,SIGNAL(released()),this,SLOT(rightBucket()));
- deleteOne_button=new QPushButton("DELETE",this);
- deleteOne_button->setGeometry(QRect(QPoint(380,325),QSize(110,77)));
- deleteOne_button->setFont(style2);
- connect(setDecimal_button,SIGNAL(released()),this,SLOT(deleteOne()));
- eight_button=new QPushButton("8",this);
- eight_button->setGeometry(QRect(QPoint(380,402),QSize(110,77)));
- eight_button->setFont(style4);
- connect(eight_button,SIGNAL(released()),this,SLOT(eight()));
- five_button=new QPushButton("5",this);
- five_button->setGeometry(QRect(QPoint(380,479),QSize(110,77)));
- five_button->setFont(style4);
- connect(five_button,SIGNAL(released()),this,SLOT(five()));
- two_button=new QPushButton("2",this);
- two_button->setGeometry(QRect(QPoint(380,556),QSize(110,77)));
- two_button->setFont(style4);
- connect(two_button,SIGNAL(released()),this,SLOT(two()));
- zero_button=new QPushButton("0",this);
- zero_button->setGeometry(QRect(QPoint(380,633),QSize(110,77)));
- zero_button->setFont(style4);
- connect(zero_button,SIGNAL(released()),this,SLOT(zero()));
- send_button=new QPushButton("=",this);
- send_button->setGeometry(QRect(QPoint(490,325),QSize(110,77)));
- send_button->setFont(style2);
- connect(send_button,SIGNAL(released()),this,SLOT(send()));
- nine_button=new QPushButton("9",this);
- nine_button->setGeometry(QRect(QPoint(490,402),QSize(110,77)));
- nine_button->setFont(style4);
- connect(nine_button,SIGNAL(released()),this,SLOT(nine()));
- six_button=new QPushButton("6",this);
- six_button->setGeometry(QRect(QPoint(490,479),QSize(110,77)));
- six_button->setFont(style4);
- connect(six_button,SIGNAL(released()),this,SLOT(six()));
- three_button=new QPushButton("3",this);
- three_button->setGeometry(QRect(QPoint(490,556),QSize(110,77)));
- three_button->setFont(style4);
- connect(three_button,SIGNAL(released()),this,SLOT(three()));
- dot_button=new QPushButton(".",this);
- dot_button->setGeometry(QRect(QPoint(490,633),QSize(110,77)));
- dot_button->setFont(style2);
- connect(dot_button,SIGNAL(released()),this,SLOT(dot()));
- }
- void Calculater::keyPressEvent(QKeyEvent *event)
- {
- if(event->key()==Qt::Key_Enter)
- Calculater::print();
- else
- {
- /*削掉shift*/
- if(((event->key() == Qt::Key_0 )||(event->key()==Qt::Key_9)) && (event->modifiers() & Qt::Key_Shift))
- formula+=(event->key()== Qt::Key_0 ?")":"(");
- else if((event->key()==Qt::Key_Backspace)||event->key()==Qt::Key_Delete)//delete
- deleteOne();
- else if(event->key()==Qt::Key_Escape)//ce
- deleteAll();
- else
- formula+=(char(event->key()));
- label->setText(formula);
- }
- }
- void Calculater::print()
- {
- QString num = "123";
- std::string num1 = num.toUtf8().constData();
- QString str = QString::fromUtf8(num1.c_str());
- label->setText(str);
- }
- void Calculater::setInteger()
- {
- formula+="Set Integer ";
- label->setText(formula);
- }
- void Calculater::setDecimal()
- {
- formula+="Set Decimal ";
- label->setText(formula);
- }
- void Calculater::setComplex()
- {
- formula+="Set Complex ";
- label->setText(formula);
- }
- void Calculater::submit()
- {
- }
- void Calculater::power()
- {
- formula+="power(";
- label->setText(formula);
- }
- void Calculater::factorial()
- {
- formula+="!";
- label->setText(formula);
- }
- void Calculater::add()
- {
- formula+="+";
- label->setText(formula);
- }
- void Calculater::min()
- {
- formula+="-";
- label->setText(formula);
- }
- void Calculater::mul()
- {
- formula+="*";
- label->setText(formula);
- }
- void Calculater::div()
- {
- formula+="/";
- label->setText(formula);
- }
- void Calculater::leftBucket()
- {
- formula+="(";
- label->setText(formula);
- }
- void Calculater::deleteAll()
- {
- formula.resize(0);
- label->setText(formula);
- }
- void Calculater::seven()
- {
- formula+="7";
- label->setText(formula);
- }
- void Calculater::four()
- {
- formula+="4";
- label->setText(formula);
- }
- void Calculater::one()
- {
- formula+="1";
- label->setText(formula);
- }
- void Calculater::rightBucket()
- {
- formula+=")";
- label->setText(formula);
- }
- void Calculater::deleteOne()
- {
- formula.resize(formula.size()-1);
- label->setText(formula);
- }
- void Calculater::eight()
- {
- formula+="8";
- label->setText(formula);
- }
- void Calculater::five()
- {
- formula+="5";
- label->setText(formula);
- }
- void Calculater::two()
- {
- formula+="2";
- label->setText(formula);
- }
- void Calculater::zero()
- {
- formula+="0";
- label->setText(formula);
- }
- void Calculater::send()
- {
- }
- void Calculater::nine()
- {
- formula+="9";
- label->setText(formula);
- }
- void Calculater::six()
- {
- formula+="6";
- label->setText(formula);
- }
- void Calculater::three()
- {
- formula+="3";
- label->setText(formula);
- }
- void Calculater::dot()
- {
- formula+=".";
- label->setText(formula);
- }
- Calculater::~Calculater()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment