I inherited a class from QObject :
class Parent: public QObject
{
Q_OBJECT
QObject* cl;
public:
Parent(QObject *parent=0):QObject(parent) {
I use CMake to manage Qt projects and the new Q_OBJECT needs to be added under the QT4_WRAP_CPP call. This will generate the moc_*.cxx for inclusion in the project and clean up the unresolved externals.
My problem was that one of my files that used a Qt macro didn't get moc'ed. I found out, that the Qt Plugin for Visual Studio doesn't recognize the Q_NAMESPACE
macro and therefore doesn't add the file to the moc'ing list.
So I used the solution from this answer to add the file to the mic'ing list:
You should find a .h file which has successfully generated "moc_*", and copy all the contents in "Custom Build Tool -> General" to the new .h file setting page.
Be careful with the different options for Debug
and Release
-Mode.
After that, build your project.
Build it once each in Debug
and Release
-Mode
Finally, add the generated "moc_*" file to your project.
Now, "moc_filename.cpp" should be in Generated Files\Debug
and Generated Files\Release
.
Right click on each of them and change thair properties:
Debug
: Change configuration to Release
and then change General->Excluded from build
to yes
.Release
: Change configuration to Debug
and then change General->Excluded from build
to yes
.Visual Studio 2017.
I've added file to already set up Qt project and got this error. How I fixed it:
Right click on the header in Solution Explorer Properties... -> Configuration Properties -> General -> Item Type Change from C/C++ Header to Qt Meta-Object Compiler (moc)
voila :)
You should delete the debug
folder of your application and run it again to correct this problem.
In my case (using QtAdd-in with VS2012 and Qt v4.8.4) none of the above suggestions worked. For some reason VS could not generate proper moc files (build output: No relevant classes found. No output generated.) and when I compiled relevant headers by hand (setting qt moc as a compiler and clicking 'Compile') it produced empty moc file.
What did work was to compile all necessary mocs from command line (moc -o moc_SomeClass.cpp SomeClass.h) and then replace the wrong ones in GeneratedFiles folder.
This is only workaround (and not a convenient one for a big project) to have your project build succesfully, but does not really explain strange VS/QtAdd-in behaviour.
I've encountered this problem with the use of a "private class" in Qt when employing the "PIMPL" (private implementation) programming pattern. Qt uses this model all through out their source code. I have come to really like it myself.
This technique involves the use of a "private" forward-declared class in a public header file, which will be be used by the "public" class (i.e. it's "parent"). The parent then has a pointer to an instance of the private class as a data member.
The "private" class is defined entirely within the cpp file for the public one. There is NO header file for the private class.
All of the "dirty work" is done with that private class. This hides all of the implementation of your public class including every other private member typically (both data and functions).
I highly recommend learning about the PIMPL pattern - especially if you are going ever read the internal Qt source.
Without explaining that coding style further, here's the point as it relates to this question... To get the Q_OBJECT
macro to work inside the cpp for the "private" class to be QObject which can use signals/slot etc., you needed to explicitly include the .moc to the public class inside the cpp:
#include "MyPublicClass.moc"
You can ignore any IDE warnings about this line.
I'm not sure if it matters exactly off hand, but that inclusion I always see done AFTER the private class definition, rather than at the top of the cpp (like includes are typically placed). So, the cpp layout goes like this: