PDA

View Full Version : Moc generates uncompilable code



Vladimir
19th January 2007, 17:22
I have a class defined inside a namespace which has properties of the type from the same namespace:



// inside the included file
Q_DECLARE_METATYPE(Core::Vector)

// inside particle.h
namespace Core {
class Particle {
Q_OBJECT
Q_PROPERTY(Core::Vector position READ position WRITE setPosition)
public:
....
};
}


But when trying to compile I'm getting the following error:
particle.moc|43| error: ‘staticMetaObject’ is not a member of ‘Core’

Inside particle.moc there is the following fragment:



static const QMetaObject *qt_meta_extradata_Core__Particle[] = {
&Core::staticMetaObject,0
};

const QMetaObject Core::Particle::staticMetaObject = {
{ &Body::staticMetaObject, qt_meta_stringdata_Core__Particle,
qt_meta_data_Core__Particle, qt_meta_extradata_Core__Particle }
};


This looks like wrong code. I have no idea why moc generates it.
Thanks in advance for any help.

Vladimir
19th January 2007, 17:47
After some more experiments I've found that the problem appears when Core::Vector and Q_DECLARE_METATYPE(Core::Vector) are in different files. When they are in the same file everything compiles OK.

Is this expected behaviour ? It seems that documentation says nothing about it.

jacek
19th January 2007, 19:10
It seems that documentation says nothing about it.
Not quite, it says:

Ideally, this macro should be placed below the declaration of the class or struct. If that is not possible, it can be put in a private header file which has to be included every time that type is used in a QVariant.
I guess the problem is that the code generated by moc doesn't include that other header file.

Vladimir
20th January 2007, 14:38
Not quite, it says:
Yes, I've missed this. Thanks for pointing me to it.


I guess the problem is that the code generated by moc doesn't include that other header file.
No, .moc file is included in .cc file which includes all required headers. Moc generates a reference to Core::staticMetaObjec where it obviously should be Core::Particle::staticMetaObject. Very strange that it is related to Q_PROPERTY.

My original goal was to put Q_DECLARE_METATYPE into different header because I want to have a subset of my API which is independent of Qt.

ChristianEhrlicher
21st January 2007, 14:59
class Particle is not derived from QObject.

Vladimir
22nd January 2007, 15:01
class Particle is not derived from QObject.
No, I's derived from QObject.

ChristianEhrlicher
22nd January 2007, 15:11
No, I's derived from QObject.

Where?



namespace Core {
class Particle {
Q_OBJECT
Q_PROPERTY(Core::Vector position READ position WRITE setPosition)
public:
....
};
}