Results 1 to 4 of 4

Thread: Simple demo won't link - undef vtable and Q_OBJECT

  1. #1
    Join Date
    Aug 2010
    Posts
    12
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Simple demo won't link - undef vtable and Q_OBJECT

    This small demo (extracted from a less small demo) won't link. I've gone over everything and am puzzled. I hope it's a simple goof, but not something too obvious (for if it _is_ obvious, that doesn't say much for my IQ today.) I'm semi-experienced with Qt4, but work entirely on an existing huge app, rarely creating new ones - until today. Now I'm trying to write a quick demo of something. But this linking problem is a brick wall to any progress.

    The line with Q_OBJECT is shown commented out - this compiles and links fine, but the slot printmyplot() is never invoked when the "print" button is clicked.

    Uncommenting the Q_OBJECT line causes the linker to complain "undefined reference to `vtable for MyMainWindow'" (four times!) and no executable is produced.

    Naturally I googled this error message, and found the most common problems were 1) failing to re-run qmake (but I have run it repeatedly, including qmake -project to be really sure); 2) some method is declared in a class but not defined (but I have defs for all my methods, all two of them); 3) classes don't override an abstract method inherited from a base class (QWidget doesn't have any, AFIK). Note that I've got all the source in one file, badslot.cpp, so the problem isn't header files not listed in .pro or the Makefile.

    What's the problem with this source?

    Contents of badslot.cpp:
    Qt Code:
    1. // uncomment the Q_OBJECT:
    2. // get undefined reference to `vtable for MyMainWindow'
    3. //
    4. // commented out: it compiles, but printmyplot() isn't called.
    5. //
    6.  
    7. #include <QtGui>
    8. #include <QWidget>
    9. #include <stdio.h>
    10. #include <unistd.h>
    11.  
    12.  
    13. class MyMainWindow : public QWidget
    14. {
    15. // Q_OBJECT /*uncomment this to get linker error */
    16.  
    17. public:
    18. MyMainWindow(QWidget *parent=0);
    19. ~MyMainWindow();
    20.  
    21. void createcontent();
    22.  
    23. public slots:
    24. void printmyplot();
    25.  
    26. private:
    27. QPushButton *print_button;
    28. QPushButton *quit_button;
    29. };
    30.  
    31. MyMainWindow::~MyMainWindow() {}
    32.  
    33. MyMainWindow::MyMainWindow(QWidget *parent)
    34. : QWidget(parent),
    35. print_button(0),
    36. quit_button(0)
    37. {
    38. resize(500,500);
    39. setWindowTitle("QWT Simple Demo #1");
    40. }
    41.  
    42.  
    43. void MyMainWindow::createcontent()
    44. {
    45. print_button = new QPushButton("print", this);
    46. quit_button = new QPushButton("quit", this);
    47.  
    48. QHBoxLayout *bottomrow = new QHBoxLayout;
    49. bottomrow->addWidget(print_button);
    50. bottomrow->addWidget(quit_button);
    51. setLayout(bottomrow);
    52.  
    53. connect(quit_button, SIGNAL(clicked()),
    54. this, SLOT(close()));
    55. connect(print_button, SIGNAL(clicked()),
    56. this, SLOT(printmyplot()));
    57.  
    58. }
    59.  
    60.  
    61. void MyMainWindow::printmyplot()
    62. {
    63. printf("entered printmyplot \n");
    64. setWindowTitle("printing...");
    65. sleep(2);
    66. setWindowTitle("(normal title goes here)");
    67. prntf("returning from printmyplot \n");
    68. }
    69.  
    70.  
    71. int main(int nargs, char** args)
    72. {
    73. QApplication app(nargs, args);
    74. MyMainWindow *mainwindow = new MyMainWindow;
    75. mainwindow->createcontent();
    76. mainwindow->show();
    77. return app.exec();
    78. }
    To copy to clipboard, switch view to plain text mode 

    The .pro file, created by qmake -project with debug manually added in, contains:
    Qt Code:
    1. TEMPLATE = app
    2. TARGET = badslot
    3. CONFIG += qt debug
    4.  
    5. DEPENDPATH += .
    6. INCLUDEPATH += .
    7. # Input
    8. SOURCES += badslot.cpp
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Simple demo won't link - undef vtable and Q_OBJECT

    First "solution" i don't really recommend this
    is to add the following line in the badslot.cpp file:
    Qt Code:
    1. #include "badslot.moc"
    To copy to clipboard, switch view to plain text mode 
    then from Build menu Run qmake then Rebuild

    But the good solution is to always have classes (even if they are not using QOBJECT macro) declared in a CLASSNAME.h and defined in CLASSNAME.cpp, then you don't need to manually include the .moc file

  3. The following user says thank you to Zlatomir for this useful post:

    DrunkenUFOPilot (15th January 2011)

  4. #3
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Simple demo won't link - undef vtable and Q_OBJECT

    You didn't declare your signals or one of your slots. You have to declare your signals and slots.
    you need something like:
    Qt Code:
    1. signals:
    2. void quit_button_clicked();
    3. void print_button_clicked();
    4.  
    5. public slots:
    6. void printmyplot();
    7. void quit_button_clicked();
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Aug 2010
    Posts
    12
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Simple demo won't link - undef vtable and Q_OBJECT

    Aha! You are right. I broke up my one source file into four, having now .hpp and .cpp. After running qmake -project, it works.

    I guess I shouldn't put "Qt guru" on my resume just yet...

Similar Threads

  1. Replies: 4
    Last Post: 28th May 2011, 22:57
  2. no moc generated , vtable link error
    By eric_vi in forum Qt Programming
    Replies: 0
    Last Post: 13th April 2010, 21:17
  3. link error LNK2001 with Q_OBJECT
    By mioan in forum Newbie
    Replies: 3
    Last Post: 18th May 2009, 00:52
  4. Q_OBJECT does'nt link
    By bnilsson in forum Qt Programming
    Replies: 2
    Last Post: 16th July 2008, 21:22
  5. Q_OBJECT macro and link errors
    By pscheven in forum Qt Programming
    Replies: 4
    Last Post: 21st March 2008, 14:23

Tags for this Thread

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.