Results 1 to 11 of 11

Thread: Issue with QObject::connect

  1. #1
    Join Date
    Feb 2012
    Posts
    4
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Issue with QObject::connect

    I'm trying to call a user-defined function while clicking a button.Here's my code and the error I'm getting

    Here's my .h file

    Qt Code:
    1. #include <QApplication>
    2. #include <QPushButton>
    3. #include <QGridLayout>
    4. #include <QLCDNumber>
    5. #include <QString>
    6. #include <iostream>
    7. #include <test.h>
    8.  
    9. int i;
    10. QString str = "0";
    11. int calc::appendone()
    12. {
    13. i = 1;
    14. str.append("1");
    15. return 0;
    16. }
    17. int main(int argc, char *argv[])
    18. {
    19. QApplication app(argc, argv);
    20. QWidget *window = new QWidget;
    21. QPushButton *one = new QPushButton("1");
    22. QPushButton *two = new QPushButton("2");
    23. QPushButton *three = new QPushButton("3");
    24. QPushButton *four = new QPushButton("4");
    25. QPushButton *five = new QPushButton("5");
    26. QPushButton *six = new QPushButton("6");
    27. QPushButton *seven = new QPushButton("7");
    28. QPushButton *eight = new QPushButton("8");
    29. QPushButton *nine = new QPushButton("9");
    30. QPushButton *zero = new QPushButton("0");
    31. QPushButton *add = new QPushButton("+");
    32. QPushButton *sub = new QPushButton("-");
    33. QPushButton *mul = new QPushButton("*");
    34. QPushButton *div = new QPushButton("/");
    35. QPushButton *equ = new QPushButton("=");
    36. QLCDNumber *lcd = new QLCDNumber(5);
    37. QObject::connect(one, SIGNAL(clicked()), &calc, SLOT(appendone()));
    38. std::cout<<i;
    39. if(i == 1)
    40. {
    41. str.append("1");
    42. lcd->display(str);
    43. }
    44. QGridLayout *layout = new QGridLayout;
    45. lcd->display(str);
    46. layout->addWidget(zero,5,1);
    47. layout->addWidget(equ,5,3);
    48. layout->addWidget(one,4, 2);
    49. layout->addWidget(two,4,1);
    50. layout->addWidget(three,4,0);
    51. layout->addWidget(add,4,3);
    52. layout->addWidget(sub,4,4);
    53. layout->addWidget(four,3,2);
    54. layout->addWidget(five,3,1);
    55. layout->addWidget(six,3,0);
    56. layout->addWidget(mul,5,3);
    57. layout->addWidget(div,5,4);
    58. layout->addWidget(seven,2,2);
    59. layout->addWidget(eight,2,1);
    60. layout->addWidget(nine,2,0);
    61. layout->addWidget(lcd,2,3);
    62. window->setLayout(layout);
    63. window->show();
    64. return app.exec();
    65. }
    To copy to clipboard, switch view to plain text mode 

    My .CPP file

    Qt Code:
    1. #include <QApplication>
    2. #include <QPushButton>
    3. #include <QGridLayout>
    4. #include <QLCDNumber>
    5. #include <QString>
    6. #include <iostream>
    7. #include <test.h>
    8.  
    9. int i;
    10. QString str = "0";
    11. int calc::appendone()
    12. {
    13. i = 1;
    14. str.append("1");
    15. return 0;
    16. }
    17. int main(int argc, char *argv[])
    18. {
    19. QApplication app(argc, argv);
    20. QWidget *window = new QWidget;
    21. QPushButton *one = new QPushButton("1");
    22. QPushButton *two = new QPushButton("2");
    23. QPushButton *three = new QPushButton("3");
    24. QPushButton *four = new QPushButton("4");
    25. QPushButton *five = new QPushButton("5");
    26. QPushButton *six = new QPushButton("6");
    27. QPushButton *seven = new QPushButton("7");
    28. QPushButton *eight = new QPushButton("8");
    29. QPushButton *nine = new QPushButton("9");
    30. QPushButton *zero = new QPushButton("0");
    31. QPushButton *add = new QPushButton("+");
    32. QPushButton *sub = new QPushButton("-");
    33. QPushButton *mul = new QPushButton("*");
    34. QPushButton *div = new QPushButton("/");
    35. QPushButton *equ = new QPushButton("=");
    36. QLCDNumber *lcd = new QLCDNumber(5);
    37. QObject::connect(one, SIGNAL(clicked()), &calc, SLOT(appendone())); //line 37
    38. std::cout<<i;
    39. if(i == 1)
    40. {
    41. str.append("1");
    42. lcd->display(str);
    43. }
    44. QGridLayout *layout = new QGridLayout;
    45. lcd->display(str);
    46. layout->addWidget(zero,5,1);
    47. layout->addWidget(equ,5,3);
    48. layout->addWidget(one,4, 2);
    49. layout->addWidget(two,4,1);
    50. layout->addWidget(three,4,0);
    51. layout->addWidget(add,4,3);
    52. layout->addWidget(sub,4,4);
    53. layout->addWidget(four,3,2);
    54. layout->addWidget(five,3,1);
    55. layout->addWidget(six,3,0);
    56. layout->addWidget(mul,5,3);
    57. layout->addWidget(div,5,4);
    58. layout->addWidget(seven,2,2);
    59. layout->addWidget(eight,2,1);
    60. layout->addWidget(nine,2,0);
    61. layout->addWidget(lcd,2,3);
    62. window->setLayout(layout);
    63. window->show();
    64. return app.exec();
    65. }
    To copy to clipboard, switch view to plain text mode 

    Here's the error I'm getting

    Qt Code:
    1. test.cpp: In function ‘int main(int, char**)’:
    2. test.cpp:37:45: error: expected primary-expression before ‘,’ token
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Issue with QObject::connect

    (You seem to have included test.cpp twice in your post, the upper one instead of test.h)

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Issue with QObject::connect

    Third parameter of QObject::connect must be an address of object. A &calc is not an address.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Issue with QObject::connect

    What is "calc"? Seems like a class and not an object. You can't get an address of a class.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2012
    Posts
    4
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Issue with QObject::connect

    Sorry.Here's my test.h file

    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include <QWidget>
    4.  
    5. using namespace std;
    6. namespace Ui {
    7. class calc;
    8. }
    9.  
    10. class calc : public QWidget {
    11. Q_OBJECT
    12. public:
    13. calc(QWidget *parent = 0);
    14. ~calc();
    15.  
    16.  
    17. protected:
    18.  
    19. private:
    20. Ui::calc *ui;
    21.  
    22. private slots:
    23.  
    24. public slots:
    25. int appendone();
    26. };
    27. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 

    I created an object for calc in my C++ file
    Qt Code:
    1. calc c;
    To copy to clipboard, switch view to plain text mode 
    Now I'm getting the following error

    Qt Code:
    1. /home/anil/Tests/Qt/test/test.cpp:10: undefined reference to `calc::calc(QWidget*)'
    2. /home/anil/Tests/Qt/test/test.cpp:10: undefined reference to `calc::~calc()'
    3. collect2: ld returned 1 exit status
    4. make: *** [test] Error 1
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Issue with QObject::connect

    You're missing a body for your constructor and your destructor. Did you implement them?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2012
    Posts
    4
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Issue with QObject::connect

    Nope I havent implemented a constructor nor a destructor. I've just declared them in the class. Should I remove the declaration?


    Added after 5 minutes:


    I defined the constructor and destructor and got the following errors
    Qt Code:
    1. test.o: In function `~calc':
    2. /home/anil/Tests/Qt/test/test.cpp:18: undefined reference to `vtable for calc'
    3. /home/anil/Tests/Qt/test/test.cpp:18: undefined reference to `vtable for calc'
    4. test.o: In function `calc':
    5. /home/anil/Tests/Qt/test/test.cpp:15: undefined reference to `vtable for calc'
    6. /home/anil/Tests/Qt/test/test.cpp:15: undefined reference to `vtable for calc'
    7. collect2: ld returned 1 exit status
    To copy to clipboard, switch view to plain text mode 


    Added after 6 minutes:


    I finally compiled the code,but now I'm getting the following error.

    Qt Code:
    1. QWidget: Must construct a QApplication before a QPaintDevice
    2. Aborted (core dumped)
    To copy to clipboard, switch view to plain text mode 

    Here's my C++ file

    Qt Code:
    1. #include <QApplication>
    2. #include <QPushButton>
    3. #include <QGridLayout>
    4. #include <QLCDNumber>
    5. #include <QString>
    6. #include <iostream>
    7. #include <test.h>
    8.  
    9. calc c;
    10.  
    11. int i;
    12. QString str = "0";
    13.  
    14. calc::calc(QWidget *parent)
    15. {
    16. }
    17. calc::~calc()
    18. {
    19. }
    20.  
    21.  
    22. int calc::appendone()
    23. {
    24. i = 1;
    25. str.append("1");
    26. return 0;
    27. }
    28. int main(int argc, char *argv[])
    29. {
    30. QApplication app(argc, argv);
    31. QWidget *window = new QWidget;
    32. QPushButton *one = new QPushButton("1");
    33. QPushButton *two = new QPushButton("2");
    34. QPushButton *three = new QPushButton("3");
    35. QPushButton *four = new QPushButton("4");
    36. QPushButton *five = new QPushButton("5");
    37. QPushButton *six = new QPushButton("6");
    38. QPushButton *seven = new QPushButton("7");
    39. QPushButton *eight = new QPushButton("8");
    40. QPushButton *nine = new QPushButton("9");
    41. QPushButton *zero = new QPushButton("0");
    42. QPushButton *add = new QPushButton("+");
    43. QPushButton *sub = new QPushButton("-");
    44. QPushButton *mul = new QPushButton("*");
    45. QPushButton *div = new QPushButton("/");
    46. QPushButton *equ = new QPushButton("=");
    47. QLCDNumber *lcd = new QLCDNumber(5);
    48. QObject::connect(one, SIGNAL(clicked()), &c, SLOT(appendone()));
    49. std::cout<<i;
    50. QGridLayout *layout = new QGridLayout;
    51. lcd->display(str);
    52. layout->addWidget(zero,5,1);
    53. layout->addWidget(equ,5,3);
    54. layout->addWidget(one,4, 2);
    55. layout->addWidget(two,4,1);
    56. layout->addWidget(three,4,0);
    57. layout->addWidget(add,4,3);
    58. layout->addWidget(sub,4,4);
    59. layout->addWidget(four,3,2);
    60. layout->addWidget(five,3,1);
    61. layout->addWidget(six,3,0);
    62. layout->addWidget(mul,5,3);
    63. layout->addWidget(div,5,4);
    64. layout->addWidget(seven,2,2);
    65. layout->addWidget(eight,2,1);
    66. layout->addWidget(nine,2,0);
    67. layout->addWidget(lcd,2,3);
    68. window->setLayout(layout);
    69. window->show();
    70. switch (i)
    71. {
    72. case 1:
    73. {
    74. str.append("1");
    75. lcd->display(str);
    76. }
    77. }
    78. return app.exec();
    79. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by fish_sticks; 28th February 2012 at 17:31.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Issue with QObject::connect

    Why is your calc object global?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Issue with QObject::connect

    you cant have global widgets - they are initialised before the qapplication.

    tip for you - you dont need globals
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  10. #10
    Join Date
    Feb 2012
    Posts
    4
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Issue with QObject::connect

    Where have I declared global widgets? Can you point out please?

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Issue with QObject::connect

    Line #9 of the last snippet.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QObject::connect
    By littlepig in forum Newbie
    Replies: 4
    Last Post: 9th February 2011, 13:08
  2. [Help] QObject::connect
    By vinny gracindo in forum Newbie
    Replies: 3
    Last Post: 20th October 2009, 14:26
  3. QObject::connect (fast)
    By baray98 in forum Qt Programming
    Replies: 1
    Last Post: 22nd February 2008, 07:30
  4. QObject::connect: No such signal
    By caseyong in forum Qt Programming
    Replies: 5
    Last Post: 19th February 2008, 08:23
  5. Replies: 4
    Last Post: 10th November 2006, 16:38

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.