[Qt-interest] Your advice about something i do.

MARTIN Pierre hickscorp at gmail.com
Fri Sep 30 14:06:23 CEST 2011


Hello dead list users,

i have a rather big program structured into plugins. Each plugin can provide different objects, and the communication between the engine and the plugin interface is done via lists of meta objects, so when a plugin loads, the engine knows about the types it exports.

The thing is, there is also an administration application which operates on objects parameters. Delegates / editors widgets are dynamically generated based on the metatype id of the parameter to edit.

So for enums, i have a very nice widget allowing single selection from a combo if it's a regular enum, and checkboxes for flags-based enums.

The question is simple: what will determine the type-id of a given object? Will the types id be generated sequentially? To know if a type is an enum without hard-testing each value, i do it like this:

// Notice the dummy classes EnumStart and EnumEnd, which are in fact "markers" i use later for testing ranges of meta types IDs.
class EnumStart {};
enum AnEnum {};
enum AnotherEnum {};
enum AndSoOn {};
class EnumEnd {};
Then:
Q_DECLARE_METATYPE(EnumStart);
Q_DECLARE_TYPEINFO(AnEnum, Q_PRIMITIVE_TYPE);
Q_DECLARE_METATYPE(AnEnum);
Q_DECLARE_TYPEINFO(AnotherEnum, Q_PRIMITIVE_TYPE);
Q_DECLARE_METATYPE(AnotherEnum);
Q_DECLARE_TYPEINFO(AndSoOn, Q_PRIMITIVE_TYPE);
Q_DECLARE_METATYPE(AndSoOn);
Q_DECLARE_METATYPE(EnumEnd);
Of course, those are also declared using the Q_ENUMS / Q_FLAGS of their belonging object.

Then at runtime:
#define EnumFullRegistration(name) qRegisterMetaType<name>(#name); qRegisterMetaTypeStreamOperators<int>(#name)
qRegisterMetaType		(EnumStart);
EnumFullRegistration	(AnEnum);
EnumFullRegistration	(AnotherEnum);
EnumFullRegistration	(AndSoOn);
qRegisterMetaType		(EnumStart);
#undef EnumFullRegistration

At run time also, i store ranges of ids in containers like that:
typedef QPair<int,int> MetaTypeIdRange;
typedef QList<MetaTypeIdRange> MetaTypeIdRangeList;
_enumMetaTypeIdRanges << MetaTypeIdRange(qMetaTypeId<EnumStart>()+1, qMetaTypeId<EnumEnd>()-1);
And when the engine loads a plugin, it asks it for it's enum range container and adds it to the known enum ranges list.

This way, my editors / delegates factory knows that when a metatype id is within the ranges, the widget to instanciate is an enum editor.

Everything is **perfectly** working. i'm not asking for any problem to solve, but i really would like your advices about this. Am i good to go? Will the metatypes ALWAYS be in a sequencial order, so my marker classes are always one type before and one type after my enums?

Thank you very much!
Pierre.

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


More information about the Qt-interest-old mailing list