QObject class and Meta Object System in Qt Framework

QObject class and Meta Object System in Qt Framework

Hello everyone, in this article we are going to talk about the relation between QObject class and meta object system within QT framework. We will know the inter-object communication.

Let's begin.

Firstly, we need to know what QObject and Meta Object system are.

QObject is the base class of all objects in QT framework. QObject contains the central functionalities of QT like signal-slot mechanism and object trees. QObject class has the Q_OBJECT macro for meta-object system.

Meta object system provides the cross-platform specification to QT. Meta object compiler read the C++ code which has written in QT framework and rewrite them into native C++. We can see the .moc files in output folders. These folders belong to meta object compiler.

We can use signal-slot mechanism, dynamic property system and runtime type information. Also it provides internationalization, we can use our strings within tr(...) methods and it lets the program to translate automatically according to device language. Meta object system provides more...

Our class need to inherit QObject class and we need to add Q_OBJECT macro to private section of class to make it meta-object. During compile time meta object compiler scan the classes and rewrite the related meta object codes with native C++ codes.

Let's take a look at what specifications provided by QObject class

Signal-Slot mechanism for inter-object communication

Signal-slot mechanism is a programming technic for communication between objects and data transferring between objects. Signal-slot is one of the most basic mechanism of Qt Framework. It defined under QObject class and we need to add Q_OBJECT macro inside private section of the class to use Signal-Slot mechanism.

Meta object compiler scans the headers at building time and looks for signal-slot connections. Then MOC rewrites the code into C++ which written with Qt framework. At build time MOC creates the object connections with Observer design pattern.

Below you can see a simple schematic of Signal-Slot mechanism Qt C++ Signal Slot Example Diagram

Object Trees

Qt framework does not have a garbage collection system. Qt handles the cleaning unreferanced objects with object trees. So object tree mechanism prevents the memory leaks and stack overflows with deleting the will not be used objects.

Let's think we create a QObject like a QDialog, QMainWindow... or another derived classes. We are going to use this class as our GUI class and we are going to organize all other model class and some GUI classes. When we create one of these classes with related parent class, it will be added inside children class of parent class. When related parent class closed and deleted, all of children classes will be erased from memory automatically.

Below image you can see the basic scheme of object tree Qt framework Object tree basic description scheme

Most of MACROS of QT Framework

In Qt there are many useful MACROs. We enable some meta object specifications for related classes with macros. Below you can see some of these macros.

Q_INTERFACES(...)
We need to define the implemented classes and plugins with Q_INTERFACE macro. You can seethe usage below.

class BasicToolsPlugin : public QObject,
                         public BrushInterface,
                         public ShapeInterface,
                         public FilterInterface
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.Examples.PlugAndPaint.BrushInterface" FILE "basictools.json")
    Q_INTERFACES(BrushInterface ShapeInterface FilterInterface)

public:
    ...
};
Q_ENUM(...)
If we need to define some enums within meta-object system, we need to declare with Q_ENUM macro right after definition. we can use it with QML mostly.

enum Priority { High, Low, VeryHigh, VeryLow };
Q_ENUM(Priority)
Q_PROPERTY(...)
We can bind a variables' reading/writing operations with some triggers. Also we can access additional operations in with Q_PROPERTY. We make them accessible from Meta-Object system with Q_PROPERTY macro.

...
    Q_PROPERTY(qreal spacing MEMBER m_spacing NOTIFY spacingChanged)
    Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged)
    ...
signals:
    void spacingChanged();
    void textChanged(const QString &newText);

private:
    qreal   m_spacing;
    QString m_text;
...
Q_INVOKABLE
We make a function as can be called from meta-object system. This macro is useful for our QML projects. Below you can see the usage of Q_INVOKABLE macro.

class Window : public QWidget
{
    Q_OBJECT

public:
    Window();
    void normalMethod();
    Q_INVOKABLE void invokableMethod();
};

That is all in this article.

Burak Hamdi TUFAN


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment.

Comments

  • There is no comment. Be the owner of first comment...