[Qt-interest] Using types from a library as Q_PROPERTY… Or the namespacing headache.

MARTIN Pierre hickscorp at gmail.com
Wed Sep 7 17:03:06 CEST 2011


Hello everybody,

Let's say in a first library project i have a namespace named RE, defining custom types like this:
namespace RE {
	typedef RELibOpt quint8 CustomType;
}
Q_DECLARE_METATYPE(RE::CustomType);

At run time (When the library loads), i properly register this type for internal use on streams and variants:
qRegisterMetaType<RE::CustomType>("RE::CustomType");
qRegisterMetaTypeStreamOperators<RE::CustomType>("RE::CustomType");

Then, i would like to use this very custom type from another project as a property. However, i'm unable to compile this object in the other project.
i am doing:
Q_PROPERTY(RE::CustomType value READ value WRITE setValue USER true NOTIFY valueChanged)

This statement, when being MOC'ed, generates this piece of code:
static const QMetaObject *qt_meta_extradata_REA__WgtCustomTypeEditor[] = { &RE::staticMetaObject, 0 };
static const QMetaObjectExtraData qt_meta_extradata2_REA__WgtCustomTypeEditor = { qt_meta_extradata_REA__WgtCustomTypeEditor, 0 };
const QMetaObject REA::WgtCustomTypeEditor::staticMetaObject = { { &QWidget::staticMetaObject, qt_meta_stringdata_REA__WgtCustomTypeEditor, qt_meta_data_REA__WgtCustomTypeEditor, &qt_meta_extradata2_REA__WgtCustomTypeEditor } };
As you can see, the qt_meta_extradata_REA__WgtCustomTypeEditor is refering to RE::staticMetaObject, which of course doesn't exist... i have no idea why it is generating such piece of code.

When i change the property declaration (And including my RE whole namespace with "using namespace RE;" before the class):
Q_PROPERTY(CustomType value READ value WRITE setValue USER true NOTIFY valueChanged)
then the code gets generated without problem and compiles, but at run-time this type of mine appears to be unknown (i get a message like "QMetaProperty::read: Unable to handle unregistered datatype 'CustomType' for property 'REA::CustomTypeEditor::value'")...

So i have digged a bit, and what i have found is that if i declare my custom type in a wrapper like this:
namespace RE {
	#ifdef Q_MOC_RUN
	class RELibOpt T {
	Q_GADGET
	public:
	#else
	namespace T {
	#endif
		typedef RELibOpt quint8 CustomType;
		extern RELibOpt const QMetaObject staticMetaObject;
	};
}

This way, everything is working as expected, but i hate having to use a RE::T::CustomType instead of a plain RE::CustomType. i also dislike the MOC preprocessor hack (which i use from times to times to enable namespaces to have a static MetaObject available)...

Can anyone please help me to make things work the way they should be?
Thanks,
Pierre.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110907/77469315/attachment.html 


More information about the Qt-interest-old mailing list