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.
Question
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.
OTHER TIPS
First of all WHY? You should write why you need this, I'm sure that your problem solution which you are trying to fix is just wrong!
Secondly problem is Q_OBJECT
macro. QEvent
is not a QObject
so this macro is not applicable and this is why you have this error.
As already pointed out: No Q_OBJECT
. But let me add something and point your attention to a rarely used and widely unknown macro: Q_GADGET
Almost a Q_OBJECT
for non-QObjects
.
From the Qt Docs:
Use Q_GADGET instead of Q_OBJECT to enable the meta object system's support for enums in a class that is not a QObject subclass. Q_GADGET makes a class member, staticMetaObject, available. staticMetaObject is of type QMetaObject and provides access to the enums declared with Q_ENUMS. Q_GADGET is provided only for C++.
Comes handy from time to time.