luffy27
4th November 2006, 08:17
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
class Calculator : public QWidget
{
Q_OBJECT
public:
Calculator(QWidget *parent=0,char *name=0 );
public slots:
void pbpushed();
int add(int,int);
int minus(int,int);
int multi(int,int);
int divide(int,int);
signals:
private:
void showresult(int);
QPushButton *pb[16];
QLineEdit *display;
int first_num;
int last_num;
int result;
QString operand;
};
#endif
calculator.cpp
#include "calculator.h"
#include <qlayout.h>
#include <qpushbutton.h>
#include <qwidget.h>
#include <qlineedit.h>
#include <qfont.h>
Calculator::Calculator(QWidget *parent,char *name)
:QWidget(parent,name)
{
display = new QLineEdit("0",this);
display->setReadOnly(true);
display->setAlignment(Qt::AlignRight);
display->setMaxLength(15);
QString str;
for(int i=0;i<10;i++){
pb[i] = new QPushButton(str.setNum(i),this);
}
pb[10] = new QPushButton("/",this);
pb[11] = new QPushButton("*",this);
pb[12] = new QPushButton("-",this);
pb[13] = new QPushButton("+",this);
pb[14] = new QPushButton("=",this);
pb[15] = new QPushButton("OFF",this);
QGridLayout *layout = new QGridLayout(this,5,4);
layout->addMultiCellWidget(display,0,0,0,3);
for(int j=1; j<10;j++){
int row = ((9-j)/3)+1;
int column = (j-1)%3;
layout->addWidget(pb[j],row,column);
}
layout->addWidget(pb[0],4,0);
layout->addWidget(pb[15],4,1);
layout->addWidget(pb[14],4,2);
layout->addWidget(pb[13],4,3);
layout->addWidget(pb[12],3,3);
layout->addWidget(pb[11],2,3);
layout->addWidget(pb[10],1,3);
for(int con=0;con<10;con++)
connect(pb[con],SIGNAL(clicked()),this,SLOT(pbpushed()));
}
void Calculator::pbpushed()
{
display->setText((QPushButton *)sender()->text());
}
main.cpp
#include <qapplication.h>
#include "calculator.h"
int main(int argc, char **argv)
{
QApplication a(argc,argv);
Calculator cal;
a.setMainWidget(&cal);
cal.show();
return a.exec();
}
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
class Calculator : public QWidget
{
Q_OBJECT
public:
Calculator(QWidget *parent=0,char *name=0 );
public slots:
void pbpushed();
int add(int,int);
int minus(int,int);
int multi(int,int);
int divide(int,int);
signals:
private:
void showresult(int);
QPushButton *pb[16];
QLineEdit *display;
int first_num;
int last_num;
int result;
QString operand;
};
#endif
calculator.cpp
#include "calculator.h"
#include <qlayout.h>
#include <qpushbutton.h>
#include <qwidget.h>
#include <qlineedit.h>
#include <qfont.h>
Calculator::Calculator(QWidget *parent,char *name)
:QWidget(parent,name)
{
display = new QLineEdit("0",this);
display->setReadOnly(true);
display->setAlignment(Qt::AlignRight);
display->setMaxLength(15);
QString str;
for(int i=0;i<10;i++){
pb[i] = new QPushButton(str.setNum(i),this);
}
pb[10] = new QPushButton("/",this);
pb[11] = new QPushButton("*",this);
pb[12] = new QPushButton("-",this);
pb[13] = new QPushButton("+",this);
pb[14] = new QPushButton("=",this);
pb[15] = new QPushButton("OFF",this);
QGridLayout *layout = new QGridLayout(this,5,4);
layout->addMultiCellWidget(display,0,0,0,3);
for(int j=1; j<10;j++){
int row = ((9-j)/3)+1;
int column = (j-1)%3;
layout->addWidget(pb[j],row,column);
}
layout->addWidget(pb[0],4,0);
layout->addWidget(pb[15],4,1);
layout->addWidget(pb[14],4,2);
layout->addWidget(pb[13],4,3);
layout->addWidget(pb[12],3,3);
layout->addWidget(pb[11],2,3);
layout->addWidget(pb[10],1,3);
for(int con=0;con<10;con++)
connect(pb[con],SIGNAL(clicked()),this,SLOT(pbpushed()));
}
void Calculator::pbpushed()
{
display->setText((QPushButton *)sender()->text());
}
main.cpp
#include <qapplication.h>
#include "calculator.h"
int main(int argc, char **argv)
{
QApplication a(argc,argv);
Calculator cal;
a.setMainWidget(&cal);
cal.show();
return a.exec();
}