Understanding QObject in Qt

Bryan Cairns (VoidRealms)
4 min readMar 3, 2021


The heart of any application built with Qt is undeniably the humble QObject class. Does not matter if you are working in a console, widgets application or a QML app. At some point you are relying on QObject to work its magic.

But how does it work, and why does it have some quirky limitations?

What makes a class a QObject?

When you first make a class you are presented with some choices, using Qt creators class wizard does not really help answer them.

The code that is generated looks something like this…

#ifndef TEST_H#define TEST_H#include <QObject>class Test : public QObject{Q_OBJECTpublic:   explicit Test(QObject *parent = nullptr);signals:};#endif // TEST_H

In contrast a normal C++ class looks like this

#ifndef TEST_H#define TEST_Hclass Test{public:};#endif // TEST_H

Clearly the additions that make our class a QObject can be boiled down to

Including QObject and inheriting it


Signals and slots

Q_OBJECT macro

Including QObject and inheriting it

This is the first major step and it’s fairly obvious, we simply inherit the public aspects of QObject. This added in all the Qt goodness we need to really make our class a QObject as far as C++ is concerned.

Notice that last part…”As far as C++ is concerned”, but its not officially a QObject…not yet…


The constructor seems simple on the surface.

explicit Test(QObject *parent = nullptr);

What we are doing here is setting up automatic memory management without having to deal with pointers. We are setting up a parent / child relationship between objects that really forms a object tree in memory. When the parent object is destroyed, all its children are destroyed as well — automatically. This is very cool as you can make the parent your application, when the application class is destroyed all its children are deleted.

Signals and Slots

I really hesitated to add this, because — you can make a class that is a QObject and never use them. Granted they do make life much easier, but they also present some challenges later on. Its because of signals and slots we can not copy a QObject — I covered this in one of my youtube videos.

Qt 6 — Episode 5 — QObjects can not be copied

The Q_Object Macro

This seemingly innocent line really does a LOT of the heavy lifting in the background. Without this macro, your class is not really a QObject, signals and slots wont work, and you might get a lot of compiler warnings and errors.

But what does it do?

At a very high level it helps kick of MOC — the Qt Meta Object Compiler — which jumps in during the build process and adds a lot of code that makes signals and slots magically work.

Its description in the documentation is simply:

“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.”

Without this macro, nothing works right. The macro is responsible for Meta Object System, Signals and Slots, and the Property System which is heavily used by QML -much of that is glued together by MOC.

Visually it would look something like this:

I covered this in a bit more detail in Qt 6 Episode 4 — QObject and Moc

If you have zero experience, I would recommend starting with Qt Core beginners. If you already know some C++ start with Qt Core Intermediate. You can also watch all the youtube videos free even without any advertisements.

Also we have a rich community of developers in the VoidRealms facebook group — Im in there all the time asking questions and helping others.

Free tutorials on youtube

Qt 5 Core for beginners with C++

Qt 5 Core Intermediate with C++

Qt 5 Core Advanced with C++

Qt 5 Widgets for beginners with C++

QML for beginners with Qt 5

Qt 5 Design Patterns

Migrating to Qt 6

Awarded Content Creator in the 2020 Qt Champion Awards:

“Bryan has a few courses available on Udemy, specifically his course on Qt 5 Design Patterns and also has a popular group on Facebook which helps others with their Qt questions as well. For his work alone in creating the Qt 5 training course online for everyone to benefit from it was easy to make him our year’s Content Creator.”

Follow me on:

YouTube: https://www.youtube.com/user/VoidRealms

Facebook: https://www.facebook.com/groups/1400884323467285

Udemy: https://www.udemy.com/user/bryan-cairns/