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

Constructor

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…

Constructor

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
https://www.youtube.com/user/VoidRealms/videos

Qt 5 Core for beginners with C++
https://www.udemy.com/course/qt-core-for-beginners/?referralCode=E2115FC6B3CC3BD742AF

Qt 5 Core Intermediate with C++
https://www.udemy.com/course/qt-core-intermediate/?referralCode=1F1A60AF279FEE1719CD

Qt 5 Core Advanced with C++
https://www.udemy.com/course/qt-core-advanced/?referralCode=8DB432110F38B253D251

Qt 5 Widgets for beginners with C++
https://www.udemy.com/course/qt-widgets-beginners/?referralCode=1B2FA9787D88D97D38D3

QML for beginners with Qt 5
https://www.udemy.com/course/qml-for-beginners/?referralCode=3B69B9927B587BBF40F1

Qt 5 Design Patterns
https://www.udemy.com/course/qt-5-design-patterns/?referralCode=3C2C41D87AB3A17B395F

Migrating to Qt 6
https://www.udemy.com/course/migrating-to-qt-6/?referralCode=F4775D810BD9551083B6

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/

--

--

Bryan Cairns (VoidRealms)
Bryan Cairns (VoidRealms)

Written by Bryan Cairns (VoidRealms)

Computer programmer loves, Qt, Python, Flutter

No responses yet