Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the Q_OBJECT macro do? Why do all Qt objects need this macro?

Tags:

c++

macros

qt

People also ask

Why is the Q_OBJECT macro required?

Actually, the Q_OBJECT macro is only required if meta-object code has to be produced by the moc tool in order to use the signals and slots mechanism, the run-time type information, the dynamic property system and translating features for internationalization.

What is macro QT?

QObject is the base class for all Qt classes, Q_OBJECT macro is used to enable meta-object features in classes and finally moc is a preprocessor that changes Q_OBJECT macro instances to C++ source code to enable meta object system mechanism in the class in which it is used.

Where is Q_OBJECT defined?

The Q_OBJECT macro must appear in the private section of a class definition that declares its own signals and slots or that uses other services provided by Qt's meta-object system.

What is Q object in Qt?

Detailed Description. QObject is the heart of the Qt Object Model. The central feature in this model is a very powerful mechanism for seamless object communication called signals and slots. You can connect a signal to a slot with connect() and destroy the connection with disconnect().


From the Qt documentation:

The Meta-Object Compiler, moc, is the program that handles Qt's C++ extensions.

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. Among other things, meta-object code is required for the signals and slots mechanism, the run-time type information, and the dynamic property system.


It simply tells the pre-compiler that this class needs to be run through the 'moc', or Meta-Object Compiler, which adds extra hidden fields and functions to the class as well as parsing signals and slots. You only need to add this to classes that use the signal/slot mechanism or other Qt class-level features, such as introspection. You do not need to add Q_OBJECT to classes that only use standard C++ features.


The MOC (meta object compiler) converts the Q_OBJECT macro included header files in to C++ equivalent source code. It basically controls the signal-slot mechanism, and makes it understandable to the C++ compiler


1 From Qt documentation of The Meta-Object System

The moc tool 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 each of those classes. This generated source file is either #include'd into the class's source file or, more usually, compiled and linked with the class's implementation.

2 From Qt documentation of THE Q_OBJECT

The Q_OBJECT macro must appear in the private section of a class definition that declares its own signals and slots or that uses other services provided by Qt's meta-object system.

3 From Qt documentation of moc

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. Among other things, meta-object code is required for the signals and slots mechanism, the run-time type information, and the dynamic property system.

4 From Qt documentation of Signals and Slots

The Q_OBJECT macro is expanded by the preprocessor to declare several member functions that are implemented by the moc; if you get compiler errors along the lines of "undefined reference to vtable for LcdNumber", you have probably forgotten to run the moc or to include the moc output in the link command.