Take the 2-minute tour ×

This link: http://doc.trolltech.com/4.5/moc.html#moc says

The moc tool reads a C++ header file. If it finds one or more class declarations that contain the Q_OBJECT macro, it produces a C++ source file containing the meta-object code for those classes.

What is a meta object code?


How to know in which classes I should write the Q_OBJECT? One example is the signals and slots, any other cases where that needs to be used?

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

Meta objects enhance programming languages by creating new or manipulate existing objects. They provide functionalities a language does not actually have by itself. The Meta Objects are interpreted either by compile time or run time. In Qt and C++ it is done during compile time by the Meta Object Compiler (moc).

An example case is the usage of the signal/slot concept.

share|improve this answer
Thanks for the answer. Please see my comment in the Donotalo's post. –  TheIndependentAquarius May 3 '11 at 7:36
It is always good to know how things work. Programmers who don't care about what is going on beneath their code are (imho) the worse programmers. But yes, the MOC lets you use funtionalities without having to implement them by yourself, so you can concentrate on using them correctly instead of making them work first. –  Exa May 3 '11 at 7:42
add comment

You can read the article Qt internals and Reversing to get in depth knowledge about Qt and its moc compiler (meta objet compiler). In summary a meta object is created by Qt's moc compiler to add extra information to a class like signal/slot mechanism etc.

share|improve this answer
Thanks for the link, I'll read that. But I just wanted 2 or 3 lines explaining the meaning of the "meta-object". –  TheIndependentAquarius May 3 '11 at 7:33
add comment

A meta object code in Qt environment is a C++ source file that is an expanded version of the C++ source file where you've put Q_OBJECT (and/or other related macros). The meta object code will have your implementation plus some other extra (meta) code so that signal and slots mechanism work.

share|improve this answer
Does this mean, that it is none of my business and I shouldn't bother looking how Q_OBJECT works? –  TheIndependentAquarius May 3 '11 at 7:35
@anisha, if you're curious of course you can dig deeper. but you can learn and use Qt without bothering about how Q_OBJECT works. however you need to learn when and why to use Q_OBJECT. –  Donotalo May 3 '11 at 7:41
In principle, yes. But if you want to use "invokeMethod" it is helpful to understand the basics of QObject and QMetaObject. –  hmuelner May 3 '11 at 7:42
@Exa , @ hmuelner Thanks to three of you for taking interest in my problem. I thought it would be important for me to know about the jargon I use in a future interview. Some one told me, that it is very important that in your code you must "know" each and every alphabet that is written there. That's why I was concerned. –  TheIndependentAquarius May 3 '11 at 7:47
add comment

A meta-object contains meta-information about an object like its name and a textual description of its signals and slots. This make it possible to call signal by "name". See the documentation about QMetaObject and this article.

share|improve this answer
add comment

Your Answer


