What does Q_OBJECT do in Qt 5.x

1

When creating an application with QUI using the Wizard of Qt Creator it creates a class for the window, similar to this:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
};

#endif // MAINWINDOW_H  

My question is: the code that appears at the beginning of the class, more precisely Q_OBJECT , What is this ?, What is it for me or what does it do ?.

    
asked by nullptr 03.08.2017 в 15:59
source

2 answers

1

Qt is a framework that works on C ++. This framework includes a series of features that are not natively supported by the language itself:

  • Metadata
  • Signs

To be able to offer these tools, Qt incorporates a stage before compiling that generates some files with complementary code to your classes ... it is about the generation of MOCs. These files incorporate metadata information (which allows you to inspect the object at run time), as well as the signal mechanism. At compile time these files are added to the project so everything works as expected.

Now, not all objects are going to have metadata or signals. The decision of who and who does not provide the macro Q_OBJECT . If you do not include this macro your object will be more like one of C ++ than one of Qt, which is not bad unless that object needs to use signals or metadata.

By the way, an indispensable requirement for the macro Q_OBJECT to do its job is for the object to inherit before or after QObject .

    
answered by 04.08.2017 / 11:22
source
0

It is a metadata of QT that serves to activate the functions of events in a QT object that is to say the use of Signals and Slots.

QT documentation on the subject

link

    
answered by 03.08.2017 в 17:32