Results 1 to 5 of 5

Thread: class not compiling because of Q_OBJECT

  1. #1
    Join Date
    May 2011
    Posts
    27
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    10

    Default class not compiling because of Q_OBJECT

    Hello,
    when I inherit from a Qt class I generally struct the class like this:

    Qt Code:
    1. class MyClass : public QDialog //or another Qt class
    2. {
    3. Q_OBJECT
    4. public:
    5. MyClass();
    6. private:
    7. QPushButton *okBtn;
    8. //....
    9. };
    To copy to clipboard, switch view to plain text mode 

    But on some classes I get the error:
    undefined reference to `vtable for MyClass'

    I don't know why Q_OBJECT sometimes works for my classes and sometimes not.

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: class not compiling because of Q_OBJECT

    Use two files per class: the class declaration in .h file and definition in .cpp file and it should work or else (if you write both definition and declaration in a cpp file) you might need to include the moc generated file, and if some particular piece of code doesn't work - post it here and we can see what is wrong with it.

  3. #3
    Join Date
    May 2011
    Posts
    27
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    10

    Default Re: class not compiling because of Q_OBJECT

    For excample this class called CreateFile.h:

    Qt Code:
    1. #ifndef CREATEFILEDIALOG_H
    2. #define CREATEFILEDIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QPushButton>
    6. #include <QRadioButton>
    7. #include <QLabel>
    8. #include <QSpinBox>
    9. #include <QComboBox>
    10. #include <QLineEdit>
    11. #include <QGroupBox>
    12. #include <QHBoxLayout>
    13. #include <QVBoxLayout>
    14.  
    15. class C_CreateFileDialog : public QDialog
    16. {
    17. //Q_OBJECT
    18. public:
    19. C_CreateFileDialog();
    20.  
    21. public slots:
    22. void createFile();
    23.  
    24. private:
    25. QLabel *describtion;
    26. QLabel *sizeLabel;
    27. QSpinBox *sizeSpin;
    28. QComboBox *sizeCombo;
    29. QGroupBox *filledWithGroup;
    30. QRadioButton *RB_Letters;
    31. QLineEdit *letters;
    32. QRadioButton *RB_AutoText;
    33.  
    34. QPushButton *createFileBtn;
    35. QPushButton *cancelBtn;
    36.  
    37. //Layouts
    38. QHBoxLayout *sizeLayout;
    39. QHBoxLayout *letterLayout;
    40. QHBoxLayout *buttonLayout;
    41. QVBoxLayout *radioLayout;
    42. QVBoxLayout *layout;
    43. };
    44.  
    45. #endif // CREATEFILEDIALOG_H
    To copy to clipboard, switch view to plain text mode 

    in the CreateFile.cpp is only the constructor in which the several items are placed and aligned to the layouts. Also the connections to the corresponding buttons are made.The createFile slot is also there but does nothing yet.

    If I comment out the Q_OBJECT the class compiles fine, but when I remove the // I get the errors.

  4. #4
    Join Date
    Apr 2010
    Posts
    769
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 94 Times in 86 Posts

    Default Re: class not compiling because of Q_OBJECT

    Do a 'make clean' on your project. Run qmake again. Run make again. See what happens.

  5. The following user says thank you to SixDegrees for this useful post:

    seux (5th June 2011)

  6. #5
    Join Date
    May 2011
    Posts
    27
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    10

    Default Re: class not compiling because of Q_OBJECT

    ah, thanks. Now its working.

Similar Threads

  1. QGraphics Item custom class not compiling
    By budda in forum Qt Programming
    Replies: 2
    Last Post: 21st April 2010, 09:56
  2. why we need Q_OBJECT???
    By phillip_Qt in forum Qt Programming
    Replies: 9
    Last Post: 26th March 2009, 08:49
  3. When do I need Q_OBJECT?
    By Morea in forum Newbie
    Replies: 1
    Last Post: 24th February 2006, 09:15
  4. Q_object???
    By superutsav in forum Newbie
    Replies: 2
    Last Post: 21st February 2006, 14:29
  5. Q_object
    By Mariane in forum Newbie
    Replies: 5
    Last Post: 7th February 2006, 18:39

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.