CopperSpice Overview  1.5.2
How Qt used moc

Moc is typically used with an input file containing class declarations similar to the following:

1 class MyClass : public QObject
2 {
3  Q_OBJECT
4 
5 public:
6  MyClass(QObject *parent = 0);
7  ~MyClass();
8 
9 signals:
10  void mySignal();
11 
12 public slots:
13  void mySlot();
14 };


In addition to signals and slots, moc also implements object properties. The Q_PROPERTY() macro declares an object property while Q_ENUMS() declare a list of enumeration types within the class, which is usable inside the Property System.

1 class MyClass : public QObject
2 {
3  Q_OBJECT
4  Q_PROPERTY(Priority priority READ priority WRITE setPriority)
5  Q_ENUMS(Priority)
6 
7  public:
8  enum Priority { High, Low, VeryHigh, VeryLow };
9 
10  MyClass(QObject *parent = 0);
11  ~MyClass();
12 
13  void setPriority(Priority priority) { m_priority = priority; }
14  Priority priority() const { return m_priority; }
15 
16  private:
17  Priority m_priority;
18 };


The Q_FLAGS() macro declares enums that are to be used as flags. Q_CLASSINFO() allows an application to attach additional name/value pairs to the class's meta-object.

1 class MyClass : public QObject
2 {
3  Q_OBJECT
4  Q_CLASSINFO("Author", "Oscar Peterson")
5  Q_CLASSINFO("Status", "Active")
6 
7  public:
8  MyClass(QObject *parent = 0);
9  ~MyClass();
10 };


If the class declaration is found in the file myclass.h, the moc output will be put in a file called moc_myclass.cpp. This file is compiled, resulting in an object file named moc_myclass.o. This object file is then included in the list of files linked together in the final building phase of your application.