Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 47

Thread: Q_OBJECT macro problem

  1. #21
    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 to make a .moc file, the #include statement on it's own is sufficient.

  2. #22
    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

    Did you set your path? Installing Qt on Windows

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

    Default Re: Q_OBJECT macro problem

    I went to Control Panel|System|Advance System Settings | Environmental Variables...|Path then typed in c:/qt/4.6.2/bin and nothing new happened.

  4. #24
    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

    I downloaded Qt for windows out of curiosity. During the installation I told it to install in a directory named "d:/qt". The moc executable (moc.exe) ended up in "d:/qt/qt/bin". There is also a "d:/qt/bin". So your path might be "c:/qt/4.6.2/qt/bin". Check to see where "moc.exe" is on your machine and put that in your path.

  5. #25
    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

    Since the windows sdk is build with two different compilers ":/qt/4.6.2/bin" is only for Qt creator, whereas :/qt/4.6.2/qt/bin is the normal bin directory of Qt.

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

    Default Re: Q_OBJECT macro problem

    No matter what I do this still happens, maybe someone could attach a video to show me what to do.

  7. #27
    Join Date
    Mar 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Q_OBJECT macro problem

    Well, i'm a brazilian guy !
    So, excuses goes for my poor english.

    My first contact with trolls and qt, was in february 2010. I am a very very very newbie.

    I am now, just wating the result of this thread.
    It is because i have installed qt creator into a linux (fedora 12) box, and noted the same situation described here.

    just proceed as follow

    1- Start a new EMPTY qt project.
    2 - Add main.cpp
    3 - Subclass any qt class and use QT_OBJECT macro.
    4. build

    The same error will ocurr.

    But if i start a non-empty project, delete all files, and then re-start adding main.cpp, the build goes right.

    Someone has any clue ?

    fe

  8. #28
    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

    This works for me, please attach your project where this error occurs.

  9. #29
    Join Date
    Mar 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Q_OBJECT macro problem

    I must reformulate my post, because i was totally wrong.

    The error occurs *ONLY* if i subclass inside main.cpp.
    Today a just started a empty Qt project an typed the following "do nothing" main.cpp :

    #include <QApplication>
    #include "SubclassE.h"

    class SubclassI : public QWidget
    {
    Q_OBJECT
    public:
    SubclassI(QWidget *parent = 0) : QWidget(parent) {} ;

    } ;

    int main(int argc, char *argv[])
    {
    SubclassE e ;
    SubclassI i ;
    QApplication a(argc, argv);

    return a.exec();
    }

    The SubclassE.h defines a class similar to SubclassI defined in the main.cpp. Here is my SubclassE.h

    #ifndef SUBCLASSE_H
    #define SUBCLASSE_H
    #include <QWidget>

    class SubclassE : public QWidget
    {
    Q_OBJECT
    public:
    SubclassE(QWidget *parent = 0) : QWidget(parent) {} ;

    } ;

    #endif // SUBCLASSE_H


    Then, when i build, the SubclassE pass through the compiler, but SubclassI (defined into main.cpp) generates
    the error :

    undefined reference to 'vtable SubclassI'

    I searched into examples code by a project that uses a subclass with Q_OBJECT macro inside main.cpp.
    I found moveblocks.

    The strange fact is that moveblocks compilation generates no errors. I saw a main.moc after the moveblocks build. That file is not generated when i build my own project. But a moc_SubclassE.cpp was generated.

    Now, the question really is :

    Why no main.moc is generated after my build ?

    thanks,
    fe.

    PS : I don't know how to put the code inside this box with that amazing colors and scrollbar.

  10. #30
    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

    For full example, the following main.cpp will both compile and link. Your SubclassE.h does not need to be modified.

    Qt Code:
    1. #include <QApplication>
    2. #include "SubclassE.h"
    3.  
    4. class SubclassI : public QWidget
    5. {
    6. Q_OBJECT
    7. public:
    8. SubclassI(QWidget *parent = 0) : QWidget(parent) {} ;
    9.  
    10. } ;
    11.  
    12. int main(int argc, char *argv[])
    13. {
    14. SubclassE e ;
    15. SubclassI i ;
    16. QApplication a(argc, argv);
    17.  
    18. return a.exec();
    19. }
    20.  
    21. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    When run, it produces the following:

    Qt Code:
    1. Starting example.exe...
    2. QWidget: Must construct a QApplication before a QPaintDevice
    To copy to clipboard, switch view to plain text mode 

    Which is expected, as you attempt to create an instance of SubclassE and SubclassI before QApplication.

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

    Quote Originally Posted by fe View Post
    PS : I don't know how to put the code inside this box with that amazing colors and scrollbar.
    Click the "Go Advanced" tab. Then hover over the icons above the text box to see what each icon provides. e.g."#" is for code.

  12. #32
    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

    @bijan311 - Please post the start up screen for your Qt Command Prompt. Qt_prompt.jpg

  13. #33
    Join Date
    Mar 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Q_OBJECT macro problem

    Yeah !

    Thanks norobro !

    I put the #include "main.moc" and build tell me that main.moc was not found.
    Then i clean the project, run qmake and rebuild it.

    There is no error now.

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

    Default Re: Q_OBJECT macro problem

    ok, here you go.

    I have no idea why the thing on top is green, its usually blue
    Attached Images Attached Images

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

    Why is Qt trying to add the codeblocks directory to the PATH?

    Maybe it's trying to use the wrong version of gcc?

    A bit heavy handed, but have you tried uninstalling CodeBlocks and then reinstalling the Qt SDK and then using Qt Creator on your project?

  16. #36
    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.

  17. #37
    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.

  18. #38
    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

  19. #39
    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 

  20. #40
    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.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.