Qt: Signals and slots Error: undefined reference to `vtable for

前端 未结 5 721
走了就别回头了
走了就别回头了 2020-12-03 16:54

Following example from this link: http://developer.kde.org/documentation/books/kde-2.0-development/ch03lev1sec3.html

#include 
#include 

        
相关标签:
5条回答
  • 2020-12-03 17:32

    This issue may be caused by some of the following reasons. I have stumbled on all of them from time to time.


    Your class header may be missing the Q_OBJECT macro. Add the macro to the header as already noted by other answers.

    class MyWidg : public QWidget
    {
        Q_OBJECT
    

    Your class header may not be parsed by the moc. Add the header file in the HEADERS definitions of your .pro (or .pri) project file.

    HEADERS += file.h
    

    If none of the above is true, then you probably need to run qmake again to make sure moc parses the header again, just in case the Q_OBJECT macro was added in the header after the qmake was run. Just run qmake again.

    0 讨论(0)
  • 2020-12-03 17:33

    It looks like moc doesn't generate code for your QObject because you declare it in the .cpp file. The easiest way to fix that is to move the declaration of MyWindow to a header, and add the header to the HEADERS list, in the .pro file:

    HEADERS += yourheader.h 
    

    Then rerun qmake.

    (Please note that the KDE 2.0 book you look at is vastly outdated)

    0 讨论(0)
  • 2020-12-03 17:41

    Maybe too late but... Had the same issue and fighted for a while to find where it comes from.

    Right click on your project and select “Run qmake” to for a new build of MOC classes. It usually does run automatically...

    The moc compiler generates the stubs and calls in moc_xxxx.cpp, and generates the vtable stuff

    0 讨论(0)
  • 2020-12-03 17:42

    Based on andref comment just above, when everything is in one cpp file like stuff.cpp, you need to add at the end of the file:

    #include "moc_stuff.cpp"
    

    (this is with Qt 4.7.0)

    0 讨论(0)
  • 2020-12-03 17:51

    Just Change your Main() as follows:

    #include <QtGui/QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MyWindow w;
    
        w.show();
    
        return a.exec();
    }
    
    0 讨论(0)
提交回复
热议问题