Page 1 of 3 123 LastLast
Results 1 to 20 of 47

Thread: Q_OBJECT macro problem

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

    Default Q_OBJECT macro problem

    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. }
    To copy to clipboard, switch view to plain text mode 
    When the compiler is linking to the .exe it gives me this error.
    Qt Code:
    1. C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\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 

    Thank you in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Q_OBJECT macro problem

    This problem did not occur when your have your class in a separate file. If you make it like you did you have to add
    Qt Code:
    1. #include "calculator.moc"
    To copy to clipboard, switch view to plain text mode 
    at the end of your code.

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

    Default Re: Q_OBJECT macro problem

    I did this and it said
    Qt Code:
    1. calculator.moc: No such file or directory
    To copy to clipboard, switch view to plain text mode 
    and there is no other file in the folder that has the .moc extension, and I have ran qmake in that directory.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Q_OBJECT macro problem

    Sorry, it has to be the name of your filename. So probably #include "main.moc"

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

    Default Re: Q_OBJECT macro problem

    ok so i put my fail name and .moc but it gives me the same error.

  6. #6
    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

    copy and paste here the exact line you typed in

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Q_OBJECT macro problem

    Have you rerun qmake in order for it to pick up the dependency that main.o now has on main.moc?
    Last edited by ChrisW67; 15th March 2010 at 00:36. Reason: Fixed wrong file name

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

    Default Re: Q_OBJECT macro problem

    yes I have rerun qmake in the directory of my program

    and here is my exact line.
    Qt Code:
    1. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Q_OBJECT macro problem

    1. show us your file
    2. How is your file named?

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

    Default Re: Q_OBJECT macro problem

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

  11. #11
    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

    Reread Lykurg's post (#2) above about where your "#include main.moc" should go.

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

    Default Re: Q_OBJECT macro problem

    you mean this?
    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 
    if so I get the same error.

  13. #13
    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

    My steps:
    1. Copy code from your post
    2. Paste into a file named main.cpp
    3. run qmake -project
    4. run qmake
    5. run make
    See the results in the attachment.

    You will still get a no such slot warning though.
    Attached Images Attached Images

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

    Default Re: Q_OBJECT macro problem

    ok I did that but when I built it it gave the same error I think it has something to do with the last line
    Attached Images Attached Images

  15. #15
    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

    The code is fine. It compiled on my Linux box.
    Might be a problem with your mingw installation. Someone familiar with windows should be able to help you out.

  16. #16
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Q_OBJECT macro problem

    it seems you are not using the mingw installation which is bundled with the sdk. Are you using Code::Blocks? then just add the right path to Qt's MinGW in the PATH variable.

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

    Default Re: Q_OBJECT macro problem

    how do I do that

  18. #18
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Q_OBJECT macro problem

    Please try first to compile your application under the Qt console. Something like "Start->Qt X.X.X->Console". There change to your directory and run qmake and mingw32-make. Does it compile?

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

    Default Re: Q_OBJECT macro problem

    the last line says
    "mingw32-make: *** [debug] ERROR 2"

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

    Default Re: Q_OBJECT macro problem

    I think the problem is I don't know how to make a .moc file

Similar Threads

  1. Problems with the Q_OBJECT-Macro
    By tokstolle in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2009, 18:09
  2. Problems with Q_OBJECT macro
    By dbrmik in forum Qt Programming
    Replies: 21
    Last Post: 26th February 2009, 15:10
  3. Q_OBJECT macro and link errors
    By pscheven in forum Qt Programming
    Replies: 4
    Last Post: 21st March 2008, 14:23
  4. Q_OBJECT macro - what exactly does it do?
    By magland in forum Qt Programming
    Replies: 3
    Last Post: 26th September 2007, 11:30
  5. Q_OBJECT macro issue
    By kandalf in forum Qt Programming
    Replies: 2
    Last Post: 23rd January 2007, 20: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.