Qt moc with implementations inside of header files?

user336063 picture user336063 · Jun 8, 2010 · Viewed 13.7k times · Source

Is it possible to tell the Qt MOC that I would like to declare the class and implement it in a single file rather than splitting them up into an .h and .cpp file?

Answer

mtvec picture mtvec · Jun 9, 2010

If you want to declare and implement a QObject subclass in you cpp file, you have to manually include the moc file.

For example: (file main.cpp)

struct SubObject : QObject
{
    Q_OBJECT
};

//...

#include "main.moc"

You have to rerun moc (make qmake) after adding the #include statement.