Results 1 to 11 of 11

Thread: Q_OBJECT error

  1. #1
    Join Date
    May 2008
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Q_OBJECT error

    Hey, my code compiles fine but I cannot connect my own functions to a signal. Is the reason because i have not put the Q_OBJECT macro in the class header file for the window?

    if so when i do put this macro in i get 2 errors.

    make: *** [debug] Error 2
    make[1]: *** [debug\buttonTest.exe] Error 1

    I was hoping if i fixed this then i would be able to connect signals to my own functions.

    -----------------

    Qt Code:
    1. class Window : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Window(QWidget *parent = 0)
    7. {
    8. button = new QPushButton("Increment", this);
    9. lcd = new QLCDNumber(2, this);
    10. slider = new QSlider(Qt::Horizontal);
    11. slider->setRange(0,99);
    12. slider->setValue(0);
    13.  
    14. QObject::connect(slider, SIGNAL(valueChanged(int)),
    15. this, SLOT(Update(int)));
    16. //lcd, SLOT(display(int)));
    17.  
    18. QVBoxLayout *layout = new QVBoxLayout;
    19. layout->addWidget(slider);
    20. layout->addWidget(lcd);
    21. layout->addWidget(button);
    22. this->setLayout(layout);
    23.  
    24. }
    25.  
    26. private slots:
    27. void Update(int i)
    28. {
    29. lcd->display(i);
    30. }
    31.  
    32.  
    33. private:
    34. QPushButton *button;
    35. QLCDNumber *lcd;
    36. QSlider *slider;
    37. };
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. //.pro file
    2. TEMPLATE = app
    3. TARGET = buttonTest
    4. QT += core \
    5. gui
    6. HEADERS += buttontest.h
    7. SOURCES += main.cpp \
    8. buttontest.cpp
    9. FORMS += buttontest.ui
    10. RESOURCES +=
    To copy to clipboard, switch view to plain text mode 
    Last edited by Toshikazu; 27th May 2008 at 15:07.

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Q_OBJECT error

    Q_OBJECT macro should be in your class definition and your function should be declared in "slots" section. In this case you'll be able to do "your connect".

  3. #3
    Join Date
    May 2008
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Q_OBJECT error

    Ok thanks.

    But when i try to include the Q_OBJECT macro i get those errors ?

  4. #4
    Join Date
    Oct 2007
    Location
    Cracow
    Posts
    56
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Q_OBJECT error

    do you place Q_OBJECT in private section of your class ?
    and remeber that your class should inherit someway from QObject class

  5. #5
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Q_OBJECT error

    There must be an earlier problem - the lines you post don't tell us anything. Also you did not tell us how you create the makefile. If you're using qmake then also show us your pro-File.

  6. #6
    Join Date
    Dec 2007
    Location
    Austin, TX
    Posts
    43
    Thanks
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Q_OBJECT error

    Did you also make sure that your class derives from QObject (either directly or indirectly)?
    Vycke

  7. #7
    Join Date
    May 2008
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Q_OBJECT error

    I have edited my original post to show the code for my class and for my .pro file. Although i am not that clear yet on the purpose of the .pro file.

  8. #8
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Q_OBJECT error

    Did you rerun qmake after you added Q_OBJECT to your header?
    And what's the actual error - as I said the error message you gave us isn't the first one you see and we can't say anything about the real problem.
    The pro-file looks fine.

    OT: But why did you put the implementation into the header? This isn't a good coding style.

  9. #9
    Join Date
    May 2008
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Q_OBJECT error

    Hi, thnanks for your time.

    I put the implementation in the header, becauase when I do not i get an additional error message. Even if it is all in the same file.

    undefined reference to `vtable for Window'

    Also I am new to eclipse and qt, This might sound dumb but am I not rerunning qmake when i save the source and build all and run? or do i need to do something else.

    Thanks

  10. #10
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Q_OBJECT error

    You have to rerun qmake every time you add/remove Q_OBJECT in an header - otherwise qmake can't create the moc calls for the headers and you get a vtable linker error.

  11. #11
    Join Date
    May 2008
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Q_OBJECT error

    How do I rerun qmake and what files do i need to pass.
    Or does qmake get invoked by eclipse everytime I do a build project?

    I tried this on cmd but I still get the errors.

    qmake -makefile -win32 C:/Eclipse/buttonTest/buttonTest.pro
    qmake -project -win32 C:/Eclipse/buttonTest/buttonTest.pro

    and they both didn't output any errors when i run them. But still errors when i try to build the project.

    Sorry my understanding is low.
    Last edited by Toshikazu; 28th May 2008 at 04:15.

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 14:22
  2. Compile 4.4.0
    By LordQt in forum Installation and Deployment
    Replies: 18
    Last Post: 29th May 2008, 13:43
  3. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 12:57
  4. Error compiling psql plugin
    By vieraci in forum Installation and Deployment
    Replies: 4
    Last Post: 7th October 2007, 02:49
  5. qt 4.2.2 install on aix
    By try to remember in forum Installation and Deployment
    Replies: 2
    Last Post: 28th March 2007, 12:19

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.