C++ simple calculator in QT4 -
i trying create simple calculator in qt4. user inputs 2 numbers , there 4 command buttons calculations. (+ - * /). code is:
calculator.h
#ifndef calculator_h #define calculator_h #include qwidget #include qgridlayout #include qlineedit #include qlabel #include qpushbutton #include qlcdnumber #include qstring #include qmessagebox #include qerrormessage class calculator : public qwidget { q_object public: //constructor calculator(); public slots: //function add 2 numbers user inputs void addition(); //function subtract 2 numbers user inputs void subtraction(); //function multiply 2 numbers user inputs void multiply(); //function divide 2 numbers user inputs void division(); //function clear widgets void clearfields(); //function close window void close(); private: qlineedit* int1entry; qlineedit* int2entry; qlcdnumber* lineoutput; qerrormessage* error; }; #endif
calculator.cpp
#include "calculator.h" //constructor calculator::calculator() { //{initializes , places widgets using layout setwindowtitle("simple calculator"); qgridlayout* layout = new qgridlayout; qpushbutton* additionbutton = new qpushbutton("+"); qpushbutton* subtractionbutton = new qpushbutton("-"); qpushbutton* multiplicationbutton = new qpushbutton("*"); qpushbutton* divisionbutton = new qpushbutton("/"); lineoutput = new qlcdnumber; //result->setbinmode(); lineoutput->setsegmentstyle(qlcdnumber::flat); lineoutput->setdigitcount(8); qpushbutton* clear = new qpushbutton("clear"); qpushbutton* close = new qpushbutton("close"); layout->addwidget(int1entry, 2,1); layout->addwidget(int2entry, 3,1); layout->addwidget(additionbutton, 0,2); layout->addwidget(subtractionbutton, 1,2); layout->addwidget(multiplicationbutton, 2,2); layout->addwidget(divisionbutton, 3,2); layout->addwidget(new qlabel, 1, 0); //for spacing layout->addwidget(lineoutput,3,0); layout->addwidget(close, 4,1); layout->addwidget(clear, 4,2); setlayout(layout); //} //{connect signals , slots connect(additionbutton, signal(clicked()), this, slot(addition())); connect(subtractionbutton, signal(clicked()), this, slot(subtraction())); connect(multiplicationbutton, signal(clicked()), this, slot(multiply())); connect(divisionbutton, signal(clicked()), this, slot(division())); connect(clear, signal(clicked()), this, slot(clearfields())); connect(close, signal(clicked()), this, slot(close())); //} } //function add 2 numbers void calculator::addition() { qstring strfisrt = int1entry->text(); qstring strsecond = int2entry->text(); bool ok; int number1 = strfisrt.toint(&ok); int number2 = strsecond.toint(&ok); int result = number1 + number2; qstring strresult; strresult = strresult.number(result); lineoutput->display(strresult); } //function subtract 2 numbers void calculator::subtraction() { qstring strfisrt = int1entry->text(); qstring strsecond = int2entry->text(); bool ok; int number1 = strfisrt.toint(&ok); int number2 = strsecond.toint(&ok); int result = number1 - number2; qstring strresult; strresult = strresult.number(result); lineoutput->display(strresult); } //function multiply 2 numbers void calculator::multiply() { qstring strfisrt = int1entry->text(); qstring strsecond = int2entry->text(); bool ok; int number1 = strfisrt.toint(&ok); int number2 = strsecond.toint(&ok); int result = number1 * number2; qstring strresult; strresult = strresult.number(result); lineoutput->display(strresult); } //function divide 2 numbers void calculator::division() { qstring strfisrt = int1entry->text(); qstring strsecond = int2entry->text(); bool ok; int number1 = strfisrt.toint(&ok); int number2 = strsecond.toint(&ok); int result = number1 / number2; qstring strresult; strresult = strresult.number(result); lineoutput->display(strresult); } //clear widgets void calculator::clearfields(){ int1entry->clear(); int2entry->clear(); lineoutput->display(0); } //close window void calculator::close(){ exit(0); }
main.cpp
#include <qapplication> #include "calculator.h" int main(int argc, char* argv[]) { qapplication app(argc, argv); calculator w; w.show(); return app.exec(); }
when run program blank ms-dos black screen. can u me please?
the problem never create instances of following items:
qlineedit* int1entry; qlineedit* int2entry;
add following beginning of constructor:
int1entry = new qlineedit; int2entry = new qlineedit;
you never assign instance of qerrormessage
error
either never use it, watch out later.
Comments
Post a Comment