| Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |
The Meta Object Compiler, moc among friends, is the program which handles Qt's C++ extensions.
The moc reads a C++ source file. If it finds one or more class declarations that contain the Q_OBJECT macro, it produces another C++ source file which contains the meta object code for the classes that use the Q_OBJECT macro. Among other things, meta object code is required for the signal/slot mechanism, runtime type information and the dynamic property system.
The C++ source file generated by the moc must be compiled and linked with the implementation of the class (or it can be #included into the class's source file).
If you use qmake to create your Makefiles, build rules will be included that call the moc when required, so you will not need to use the moc directly. For more background information on moc, see Why doesn't Qt use templates for signals and slots?.
The moc is typically used with an input file containing class declarations like this:
class MyClass : public QObject
{
Q_OBJECT
public:
MyClass( QObject * parent=0, const char * name=0 );
~MyClass();
signals:
void mySignal();
public slots:
void mySlot();
};
In addition to the signals and slots shown above, the moc also implements object properties as in the next example. The Q_PROPERTY macro declares an object property, while Q_ENUMS declares a list of enumeration types within the class to be usable inside the property system. In this particular case we declare a property of the enumeration type Priority that is also called "priority" and has a get function priority() and a set function setPriority().
class MyClass : public QObject
{
Q_OBJECT
Q_PROPERTY( Priority priority READ priority WRITE setPriority )
Q_ENUMS( Priority )
public:
MyClass( QObject * parent=0, const char * name=0 );
~MyClass();
enum Priority { High, Low, VeryHigh, VeryLow };
void setPriority( Priority );
Priority priority() const;
};
Properties can be modified in subclasses with the Q_OVERRIDE macro. The Q_SETS macro declares enums that are to be used as sets, i.e. OR'ed together. Another macro, Q_CLASSINFO, can be used to attach additional name/value-pairs to the class' meta object:
class MyClass : public QObject
{
Q_OBJECT
Q_CLASSINFO( "Author", "Oscar Peterson")
Q_CLASSINFO( "Status", "Active")
public:
MyClass( QObject * parent=0, const char * name=0 );
~MyClass();
};
The three concepts, signals and slots, properties and class meta-data, can be combined.
The output produced by the moc must be compiled and linked, just like the other C++ code in your program; otherwise the build will fail in the final link phase. By convention, this is done in one of the following two ways:
#include "myclass.moc"
at the end. This will cause the moc-generated code to be compiled and
linked together with the normal class definition in myclass.cpp, so
it is not necessary to compile and link it separately, as in Method A.
Method A is the normal method. Method B can be used in cases where you want the implementation file to be self-contained, or in cases where the Q_OBJECT class is implementation-internal and thus should not be visible in the header file.
For anything but the simplest test programs, it is recommended that you automate running the moc. By adding some rules to your program's Makefile, make can take care of running moc when necessary and handling the moc output.
We recommend using Trolltech's free makefile generation tool, qmake, for building your Makefiles. This tool recognizes both Method A and B style source files, and generates a Makefile that does all the necessary moc handling.
If you want to create your Makefiles yourself, here are some tips on how to include moc handling.
For Q_OBJECT class declarations in header files, here is a useful makefile rule if you only use GNU make:
moc_%.cpp: %.h
moc $< -o $@
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to write portably, ont
If you want to w