Understanding QObject in Qt

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.

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

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.


The constructor seems simple on the surface.

explicit Test(QObject *parent = nullptr);

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.

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.

Awarded Content Creator in the 2020 Qt Champion Awards:



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bryan Cairns (VoidRealms)

Bryan Cairns (VoidRealms)

Computer programmer loves, Qt, Python, Flutter