PDA

View Full Version : question about the use of sender( ) function



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

munna
4th November 2006, 08:25
try




QPushButton *pb = (QPushButton *)sender();

if(pb){
display->setText(pb->text());
}

luffy27
4th November 2006, 08:39
try




QPushButton *pb = (QPushButton *)sender();

if(pb){
display->setText(pb->text());
}



I tied you method. But it happened some errors when compiling.
Here is the error report:

g++ -c -pipe -Wall -W -O2 -g -pipe -m32 -march=i386 -mtune=pentium4 -DQT_NO_DEBUG -DQT_SHARED -DQT_THREAD_SUPPORT -I/usr/lib/qt-3.3/mkspecs/default -I. -I. -I/usr/lib/qt-3.3/include -o calculator.o calculator.cpp
g++ -o calculator calculator.o main.o -L/usr/lib/qt-3.3/lib -L/usr/X11R6/lib -lqt-mt -lXext -lX11 -lm
calculator.o(.text+0x33): In function `Calculator::Calculator(QWidget*, char*)':
/home/calculator/calculator.cpp:10: undefined reference to `vtable for Calculator'
calculator.o(.text+0x3a):/home/calculator/calculator.cpp:10: undefined reference to `vtable for Calculator'
calculator.o(.text+0x6f5): In function `Calculator::Calculator(QWidget*, char*)':
/home/calculator/calculator.cpp:10: undefined reference to `vtable for Calculator'
calculator.o(.text+0x6fc):/home/calculator/calculator.cpp:10: undefined reference to `vtable for Calculator'
main.o(.text+0x70): In function `main':
/home/calculator/main.cpp:10: undefined reference to `vtable for Calculator'
main.o(.text+0x7c):/home/calculator/main.cpp:10: more undefined references to `vtable for Calculator' follow
collect2: ld returned 1 exit status
make: *** [calculator] error 1

luffy27
4th November 2006, 09:15
I known where is the problem. The errors are resulted by some undefined public slots.