PDA

View Full Version : Why does qmake/moc only process header files?



doberkofler
2nd March 2010, 17:56
I was wondering, why qmake/moc only processes header (*.h) files and not source (*.cpp) files?
I just wonted to declare a class locally in a source module and discovered, that no moc files have been generated.
Why this restriction and how should it be worked around?
Thank you,
Dieter

pitonyak
2nd March 2010, 20:43
It only looks at *.h files because that is where classes are usually defined. The work around is to define your "private" class in a .h file.

squidge
2nd March 2010, 20:49
If you want a private class that is local to a file, then create a private header file only included by that source file. For example, myfile_internal.h and define your classes there.

wysota
2nd March 2010, 21:35
There is a better way. If your implementation file that contains a local class is called xyz.cpp then write this under the class declaration:

#include "xyz.moc"
This instructs qmake to force running moc on xyz.cpp. Remember to rerun qmake after you add this statement.


class MyLocalClass : public QObject {
Q_OBJECT
public:
// ...
};

#include "myfile.moc"

doberkofler
3rd March 2010, 08:21
I'm not sure if I understand you correctly. Does the include xyz.moc have to to exist and what should it contain?

squidge
3rd March 2010, 08:58
I believe the only reason it is there is that when the makefile is rebuilt it will automatically create a rule to parse the cpp file using MOC, as qmake will parse cpp files for #include lines for dependancy purposes.

Some people also state that doing so can reduce compilation time: http://edu.kde.org/development/tips.php#Whatistheuseofcodeincludemyfilemoccode
There some discussion about it on the Qt interest archive: http://lists.trolltech.com/qt-interest/2003-09/thread00793-0.html
And it seems to be used more in KDE: http://www.kdevelop.org/3.0/doc/tutorial_settings/#p1

wysota
3rd March 2010, 09:32
For me it's just a matter of convinience that I don't have to create separate header files just to declare local classes.