Results 1 to 20 of 47

Thread: Q_OBJECT macro problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Q_OBJECT macro problem

    Sorry bijan311 I've just been shooting in the dark. I haven't used windows for several years and this is my first stab at using Qt under windows. Heck I had to squirt a little wd40 onto my winxp to get it to boot

    Looks to me like the path still isn't right. Notice that my path is set to d:\Qt\4.6.2\qt\bin. That's where moc.exe resides on my system. I have no idea how it was set if changing the path in environment variables didn't change yours.

    Fatjuicymole is a windows user so I'd follow his advice.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Q_OBJECT macro problem

    My post is a complete stab in the dark too, but it just seems weird that since Qt comes with GCC, why it would use the GCC in the Codeblocks directory.

  3. #3
    Join Date
    Feb 2010
    Posts
    42
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Q_OBJECT macro problem

    Maybe because my MinGW installment is in the Codeblocks directory

  4. #4
    Join Date
    Feb 2010
    Posts
    42
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Q_OBJECT macro problem

    Ok I found "main.moc" in the debug folder so I copied that into the folder that main.cpp is in, but when i compiles and i press execute the LCDNumber doesn't change.
    here is my code
    Qt Code:
    1. #include <QApplication>
    2. #include <QPushButton>
    3. #include <QLabel>
    4. #include <QSpinBox>
    5. #include <QLCDNumber>
    6. #include <QHBoxLayout>
    7. #include <QVBoxLayout>
    8.  
    9. class Calculator : public QWidget {
    10. Q_OBJECT
    11. public:
    12. Calculator(QWidget *parent = 0) : QWidget(parent){
    13. minus = new QLabel("-");
    14. equal = new QPushButton("&Execute");
    15. num1 = new QSpinBox;
    16. num2 = new QSpinBox;
    17. answer = new QLCDNumber(3);
    18.  
    19. QHBoxLayout *layout = new QHBoxLayout;
    20. layout->addWidget(num1);
    21. layout->addWidget(minus);
    22. layout->addWidget(num2);
    23. layout->addWidget(answer);
    24. QHBoxLayout *buttons = new QHBoxLayout;
    25. buttons->addStretch();
    26. buttons->addWidget(equal);
    27. buttons->addStretch();
    28. QVBoxLayout *mainLayout = new QVBoxLayout(this);
    29. mainLayout->addLayout(layout);
    30. mainLayout->addLayout(buttons);
    31. QObject::connect(equal, SIGNAL(clicked()), answer, SLOT(calculate()));
    32. }
    33. public slots:
    34. void calculate() {
    35. answer->display(num1->value()-num2->value());
    36. disconnect();
    37. }
    38. private:
    39. QLabel *minus;
    40. QPushButton *equal;
    41. QPushButton *help;
    42. QSpinBox *num1;
    43. QSpinBox *num2;
    44. QLCDNumber *answer;
    45. };
    46.  
    47. int main(int argc, char **argv){
    48. QApplication app(argc, argv);
    49. Calculator w;
    50. w.show();
    51. return app.exec();
    52. }
    53. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Q_OBJECT macro problem

    Your connect call is still wrong. Your answer object doesn't have a slot called calculate.

  6. #6
    Join Date
    Feb 2010
    Posts
    42
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Q_OBJECT macro problem

    but its in public slots

  7. #7
    Join Date
    Aug 2009
    Location
    Greece, Chania
    Posts
    63
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Q_OBJECT macro problem

    It's in the public slots of class Calculator and NOT of class QLCDNumber. So change your connect statement to this:
    Qt Code:
    1. QObject::connect(equal, SIGNAL(clicked()), this, SLOT(calculate()));
    To copy to clipboard, switch view to plain text mode 
    Misha R.evolution - High level Debugging IDE

    Programming is about 2 basic principles: KISS and RTFM!!!

  8. The following user says thank you to Archimedes for this useful post:

    bijan311 (26th March 2010)

  9. #8
    Join Date
    Feb 2010
    Posts
    42
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Q_OBJECT macro problem

    thanks so much

  10. #9
    Join Date
    Feb 2010
    Posts
    42
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Q_OBJECT macro problem

    wait... now I changed the code up a bit and I think need to update the .moc file but can't find out how
    he is main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "calc.h"
    3.  
    4. int main(int argc, char **argv){
    5. QApplication app(argc, argv);
    6. Calculator w;
    7. w.show();
    8. return app.exec();
    9. }
    10. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    and calc.h
    Qt Code:
    1. #ifndef CALC_H
    2. #define CALC_H
    3.  
    4. #include <QPushButton>
    5. #include <QLabel>
    6. #include <QSpinBox>
    7. #include <QLCDNumber>
    8. #include <QHBoxLayout>
    9. #include <QVBoxLayout>
    10.  
    11. class Calculator : public QWidget {
    12. Q_OBJECT
    13. public:
    14. Calculator(QWidget *parent = 0) : QWidget(parent){
    15. minus = new QPushButton("&-");
    16. add = new QPushButton("&+");
    17. multiply = new QPushButton("&X");
    18. divide = new QPushButton("&/");
    19. num1 = new QSpinBox;
    20. num2 = new QSpinBox;
    21. answer = new QLCDNumber(3);
    22.  
    23. QHBoxLayout *layout = new QHBoxLayout;
    24. layout->addWidget(num1);
    25. layout->addWidget(num2);
    26. layout->addWidget(answer);
    27.  
    28. QHBoxLayout *button1 = new QHBoxLayout;
    29. button1->addStretch();
    30. button1->addWidget(add);
    31. button1->addWidget(minus);
    32. button1->addStretch();
    33.  
    34. QHBoxLayout *button2 = new QHBoxLayout;
    35. button2->addStretch();
    36. button2->addWidget(multiply);
    37. button2->addWidget(divide);
    38. button2->addStretch();
    39.  
    40.  
    41. QVBoxLayout *mainLayout = new QVBoxLayout(this);
    42. mainLayout->addLayout(layout);
    43. mainLayout->addLayout(button1);
    44. mainLayout->addLayout(button2);
    45.  
    46. connect(minus, SIGNAL(clicked()), this, SLOT(subtract()));
    47.  
    48. }
    49. public slots:
    50. void subtract(){
    51. answer->display(num1->value()-num2->value());
    52. disconnect();
    53. }
    54. void plus(){
    55. answer->display(num1->value()+num2->value());
    56. }
    57. void times(){
    58. answer->display(num1->value()*num2->value());
    59. }
    60. void divider(){
    61. answer->display(num1->value()/num2->value());
    62. }
    63. private:
    64. QPushButton *minus;
    65. QPushButton *multiply;
    66. QPushButton *divide;
    67. QPushButton *help;
    68. QSpinBox *num1;
    69. QSpinBox *num2;
    70. QLCDNumber *answer;
    71. };
    72. #endif CALC_H
    To copy to clipboard, switch view to plain text mode 

  11. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Q_OBJECT macro problem

    You don't need the #include anymore as your class is in a header file now. Just run qmake.

  12. #11
    Join Date
    Feb 2010
    Posts
    42
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Q_OBJECT macro problem

    but if i take out
    Qt Code:
    1. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    I get this error
    Qt Code:
    1. C:\MinGW\include\c++\3.4.5\bits\stl_algobase.h:(.text$_ZN10CalculatorD1Ev[Calculator::~Calculator()]+0xb)||undefined reference to `vtable for Calculator'|
    To copy to clipboard, switch view to plain text mode 

  13. #12
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Q_OBJECT macro problem

    Yes, which is why you need to run qmake, to update the project file to resolve that error.

Similar Threads

  1. Problems with the Q_OBJECT-Macro
    By tokstolle in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2009, 17:09
  2. Problems with Q_OBJECT macro
    By dbrmik in forum Qt Programming
    Replies: 21
    Last Post: 26th February 2009, 14:10
  3. Q_OBJECT macro and link errors
    By pscheven in forum Qt Programming
    Replies: 4
    Last Post: 21st March 2008, 13:23
  4. Q_OBJECT macro - what exactly does it do?
    By magland in forum Qt Programming
    Replies: 3
    Last Post: 26th September 2007, 10:30
  5. Q_OBJECT macro issue
    By kandalf in forum Qt Programming
    Replies: 2
    Last Post: 23rd January 2007, 19:28

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
  •  
Qt is a trademark of The Qt Company.