C++ class declaration syntax in Qt

I am using Qt-6 for the first time and am relatively Amateur in C++. I saw this type of Class declaration and am not able to understand what it means.

class XYZ : public QWidget
{
    Q_OBJECT
    public:
        XYZ
...
};

What is this called ? Can someone explain what this is or point to relevant material / question.

Answer

This is a class declaration in which your class XYZ inherits from a QT built-in class called QWidget which is the the base class of all user interface objects in QT (cf: https://doc.qt.io/qt-6/qwidget.html ).

In brief, the macro Q_OBJECT allows you to use the system of signals and slots ( used for communication between objects in QT) among other thing (cf: https://doc.qt.io/qt-6/qobject.html#Q_OBJECT) The documentation states it like this :

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.

Leave a Reply

Your email address will not be published. Required fields are marked *