Search code examples
c++qtqobject

QObject error with macro and include


I want to use signal and slot in my program and for this I am told Ineed to add Q_OBJECT as below.

Well I have a class:

class A
{
    Q_OBJECT
public:
    A();
};

This gives an error which says 'Q_OBJECT does not name a type'. If I than add #include It give the error 'undefined reference to vtable of A'

So what is the right way to do this?


Solution

  • The Q_OBJECT macro is meant for subclasses of a QObject (or other subclasses). It is also required if you want your class to use signals and slots.

    class A 
        : public QObject
    {
        Q_OBJECT
    
     public:
        A(QObject *parent = 0);
    };
    

    Q_OBJECT

    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.
    ...
    Note:
    This macro requires the class to be a subclass of QObject. ...