8.4. Q_OBJECT and moc: A checklist

[ fromfile: qobject.xml id: moc ]

QObject supports features not normally available in C++ objects.

Some of these features are only possible through the use of generated code. The Meta Object Compiler, moc, generates additional functions for each QObject-derived class that uses the Q_OBJECT macro. Generated code can be found in files with names moc_filename.cpp.

This means that some errors from the compiler/linker may be confuscated[67] when moc cannot find or process a class in the project. To help ensure that moc processes each QObject-derived class in the project, following are some guidelines for writing C++ code and qmake project files.

[Note] Multiple Inheritance and QObject

Because each Q_OBJECT macro generates code, it needs to be preprocessed by moc. moc works under the assumption that you are only deriving from QObject once and, further, that QObject is the first base class in the list of base classes. If you accidentally inherit from QObject more than once, or if it is not the first base class in the inheritance list, you may produce strange errors in the moc-generated code.

[Note] Undefined reference to vtable

If you define a QObject-derived class, build an application, realize that you need to add the Q_OBJECT macro to the class definition, and add it after the project was built with an old Makefile, you must then rerun qmake to update the Makefile.

make is not smart enough to add the moc step on such files to your Makefile otherwise. A "clean rebuild" does not fix this problem usually. This is an issue that often causes headaches for inexperienced Qt developers. For more information about this error message, see Section C.3.1



[67] confusing + obfuscated