PDA

View Full Version : what is a moc file?



meozeren
20th July 2009, 12:17
hi,

i noticed that qt creates a file named moc_projectname.cpp and i wonder what this file is.

thanks

wagmare
20th July 2009, 12:39
source from C++ GUI Programming with Qt4 book


One of Qt's major achievements has been the extension of C++ with a mechanism for creating independent software components that can be bound together without any component knowing anything about the other components it is connected to.

The mechanism is called the meta-object system, and it provides two key services: signals–slots and introspection. The introspection functionality is necessary for implementing signals and slots, and allows application programmers to obtain "meta-information" about QObject subclasses at run-time, including the list of signals and slots supported by the object and its class name. The mechanism also supports properties (used extensively by Qt Designer) and text translation (for internationalization), and it lays the foundation for the QtScript module.

Standard C++ doesn't provide support for the dynamic meta-information needed by Qt's meta-object system. Qt solves this problem by providing a separate tool, moc, that parses Q_OBJECT class definitions and makes the information available through C++ functions. Since moc implements all its functionality using pure C++, Qt's meta-object system works with any C++ compiler.

The mechanism works as follows:

The Q_OBJECT macro declares some introspection functions that must be implemented in every QObject subclass: metaObject(), tr(), qt_metacall(), and a few more.

Qt's moc tool generates implementations for the functions declared by Q_OBJECT and for all the signals.

QObject member functions such as connect() and disconnect() use the introspection functions to do their work.
All of this is handled automatically by qmake, moc, and QObject, so you rarely need to think about it. But if you are curious, you can read the QMetaObject class documentation and have a look at the C++ source files generated by moc to see how the implementation works.

also this
http://doc.trolltech.com/4.1/qmetaobject.html