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

心不动则不痛 提交于 2019-11-27 15:00:20
Frank Osterfeld

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)

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

anj

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();
}

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)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!