I inherited a class from QObject :
class Parent: public QObject
{
Q_OBJECT
QObject* cl;
public:
Parent(QObject *parent=0):QObject(parent) {
I added cpp/ui files to my project manually, but forgot to add the header file explicitly as header file. Now when compiling I got a similar error message as above and the moc_*.cpp file(s) were not generated in the debug (or release) directory of the build. That was not such an obvious mistake, qmake did not complain and other than the linker message I got no errors.
So if anyone encounters the same problem again (or makes the same copy & pase mistake): make sure the header files have also been added to your project file
I noticed some answers are based on Visual Studio.
This answer is based on Qt Creator.
Unlike the name suggest, Rebuild Project
will not wipe out everything and build from scratch. If you recently added QObject
(and/or Q_OBJECT) to your class, you'll have to run qmake
again, e.g.
This is because, by default, qmake
only runs when you do significant changes to your solution like adding new source files or modify the .pro
file. If you make edits to an existing file, it doesn't know it needs to run qmake
.
As a fall back, to brute force Qt to build everything from scratch, delete the Debug
or Release
folder.
If your moc files are generated in the visual studio project try to include them into project if they are not included into project then rebuild.
I had the same problem in Visual Studio, and solved it by taking the following steps:
Then in the Custom Build Tool configuration:
set "Command Line" to:
"$(QTDIR)\bin\moc.exe" "%(FullPath)" -o ".\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp" "-fStdAfx.h" "-f../../../src/FileName.h" -DUNICODE -DWIN32 -DWIN64 -DQT_DLL -DQT_NO_DEBUG -DNDEBUG -DQT_CORE_LIB -DQT_GUI_LIB -DQT_WIDGETS_LIB -DQT_NETWORK_LIB -DWIN32_LEAN_AND_MEAN -DDIS_VERSION=7 -D_MATH_DEFINES_DEFINED "-I.\SFML_STATIC" "-I.\GeneratedFiles" "-I." "-I$(QTDIR)\include" "-I.\GeneratedFiles\$(ConfigurationName)." "-I$(QTDIR)\include\QtCore" "-I$(QTDIR)\include\QtGui" "-I$(QTDIR)\include\QtNetwork"
set "Outputs" to:
.\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp
set "Additional Dependencies" to:
$(QTDIR)\bin\moc.exe;%(FullPath)
Your exact values may be different. They are usually applied via a Qt plugin.
Using QtAdd-in with VS2010 i realized the moc_*.cpp files were updated in the GeneratedFiles/Debug folder although i was in release mode. Copying the files into the Release folder worked for me.
I had this problem with Visual Studio 2012 when I had a Q_OBJECT class definition in my cpp file. Moving the class definition to the header file resolved the issue.
It looks like it should be possible to support Q_OBJECT class in cpp file by adding the cpp file to moc but I did not try this.