Search code examples
qtqt5qevent

Can I subclass QEvent?


I tried to make a class which is a subclass of QEvent, but I got error after building.

My steps,
1. Create a project using Qt console template
2. create the following code

#ifndef MYEVENT_H
#define MYEVENT_H

#include <QEvent>
#include <QObject>

class MyEvent : public QEvent
{
    Q_OBJECT
public:
    explicit MyEvent();

signals:

public slots:
};

#endif

//CPP File
MyEvent::MyEvent() :
    QEvent(QEvent::User)
{
}

moc_MyEvent.cpp:70:21: error: invalid use of non-static data member 'd_ptr'
    return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject;
           ~~~~~~~~~^~~~~
moc_MyEvent.cpp:70:21: error: 'd_ptr' is a protected member of 'QObject'
../../../../../../Qt5.1.0/5.1.0/clang_64/include/QtCore/qobject.h:411:33: note: declared protected here
    QScopedPointer<QObjectData> d_ptr;
                            ^

Qt5
Mac OSX 10.8.4


How do I solve it and why? Thanks.


Solution

  • Dcow gives corrent answer.

    Your mistake is that QEvent does not inherit from QObject, and you try to do it. You should not use Q_OBJECT macros or you should interhit your class from QObject too. But it's dark side.