Following example from this link: http://developer.kde.org/documentation/books/kde-2.0-development/ch03lev1sec3.html
#include
#include
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.
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
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)
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();
}