I'm programing a simple calculator on Qt3 within Linux.But still not complete.
In the inplement function pbpushed(), the sender() doesn't work. I know the sender() will return a QOject* type which has not a member function text( ) and there is not some method like qobject_cast ( QObject * object ) in Qt4. BUT WHAT SHOULD I DO to recognize which button is pushed in the pbpushed() .Thanks a lot.
PLUS: I'm programing with Qt3.
Here is the code:

calculator.h

Qt Code:
  1. class Calculator : public QWidget
  2. {
  3. Q_OBJECT
  4. public:
  5. Calculator(QWidget *parent=0,char *name=0 );
  6. public slots:
  7. void pbpushed();
  8. int add(int,int);
  9. int minus(int,int);
  10. int multi(int,int);
  11. int divide(int,int);
  12. signals:
  13. private:
  14. void showresult(int);
  15. QPushButton *pb[16];
  16. QLineEdit *display;
  17. int first_num;
  18. int last_num;
  19. int result;
  20. QString operand;
  21. };
  22. #endif
To copy to clipboard, switch view to plain text mode 

calculator.cpp

Qt Code:
  1. #include "calculator.h"
  2. #include <qlayout.h>
  3. #include <qpushbutton.h>
  4. #include <qwidget.h>
  5. #include <qlineedit.h>
  6. #include <qfont.h>
  7.  
  8. Calculator::Calculator(QWidget *parent,char *name)
  9. :QWidget(parent,name)
  10. {
  11. display = new QLineEdit("0",this);
  12. display->setReadOnly(true);
  13. display->setAlignment(Qt::AlignRight);
  14. display->setMaxLength(15);
  15.  
  16. QString str;
  17. for(int i=0;i<10;i++){
  18. pb[i] = new QPushButton(str.setNum(i),this);
  19. }
  20. pb[10] = new QPushButton("/",this);
  21. pb[11] = new QPushButton("*",this);
  22. pb[12] = new QPushButton("-",this);
  23. pb[13] = new QPushButton("+",this);
  24. pb[14] = new QPushButton("=",this);
  25. pb[15] = new QPushButton("OFF",this);
  26.  
  27. QGridLayout *layout = new QGridLayout(this,5,4);
  28. layout->addMultiCellWidget(display,0,0,0,3);
  29.  
  30. for(int j=1; j<10;j++){
  31. int row = ((9-j)/3)+1;
  32. int column = (j-1)%3;
  33. layout->addWidget(pb[j],row,column);
  34. }
  35. layout->addWidget(pb[0],4,0);
  36. layout->addWidget(pb[15],4,1);
  37. layout->addWidget(pb[14],4,2);
  38. layout->addWidget(pb[13],4,3);
  39. layout->addWidget(pb[12],3,3);
  40. layout->addWidget(pb[11],2,3);
  41. layout->addWidget(pb[10],1,3);
  42. for(int con=0;con<10;con++)
  43. connect(pb[con],SIGNAL(clicked()),this,SLOT(pbpushed()));
  44. }
  45. void Calculator::pbpushed()
  46. {
  47. display->setText((QPushButton *)sender()->text());
  48. }
To copy to clipboard, switch view to plain text mode 

main.cpp
Qt Code:
  1. #include <qapplication.h>
  2. #include "calculator.h"
  3.  
  4. int main(int argc, char **argv)
  5. {
  6. QApplication a(argc,argv);
  7. Calculator cal;
  8. a.setMainWidget(&cal);
  9. cal.show();
  10. return a.exec();
  11. }
To copy to clipboard, switch view to plain text mode