Results 1 to 4 of 4

Thread: question about the use of sender( ) function

  1. #1
    Join Date
    Nov 2006
    Posts
    58
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default question about the use of sender( ) function

    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 

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: question about the use of sender( ) function

    try

    Qt Code:
    1. QPushButton *pb = (QPushButton *)sender();
    2.  
    3. if(pb){
    4. display->setText(pb->text());
    5. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Nov 2006
    Posts
    58
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: question about the use of sender( ) function

    Quote Originally Posted by munna View Post
    try

    Qt Code:
    1. QPushButton *pb = (QPushButton *)sender();
    2.  
    3. if(pb){
    4. display->setText(pb->text());
    5. }
    To copy to clipboard, switch view to plain text mode 
    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

  4. #4
    Join Date
    Nov 2006
    Posts
    58
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: question about the use of sender( ) function

    I known where is the problem. The errors are resulted by some undefined public slots.

Similar Threads

  1. Link Errors
    By magikalpnoi in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2006, 22:04
  2. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52
  3. Qt 4.1.4 plugin QPSQL
    By jcr in forum Installation and Deployment
    Replies: 4
    Last Post: 22nd June 2006, 22:55
  4. I got two problems when I used static compiled library of QT4
    By qintm in forum Installation and Deployment
    Replies: 8
    Last Post: 20th April 2006, 08:52
  5. Replies: 25
    Last Post: 15th January 2006, 00:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.