PDA

View Full Version : Q_ENUMS on C-style global enums



luf
8th July 2008, 16:05
Hi

I'm using external headers (C-style) that have enums outside classes.

I was hoping to use the Q_ENUMS to get the strings directly, but haven't found a solution.

As it's C and no C++, the enums are global.

I cannot seem to find a way to use Q_ENUMS for global enums outside a namespace, or does anyone know a way to make it work?

cheers,
Leif

jpn
8th July 2008, 18:44
I'm afraid there is simply no way to do that. Q_ENUMS registers enums to the meta-object of that particular class. There is no meta-object for the global namespace... :)

In addition, the class defining the enum has to inherit QObject.
In fact, you can use Q_GADGET together with Q_ENUMS in a non-QObject subclass... but again, I don't think there is any solution to your problem.

luf
8th July 2008, 20:11
Thanks for the reply.

I found a solution, by modifing the header that contained the enums with something similar to this:


#ifdef QT
class MyQtAppEnums : public QObject
{
Q_OBJECT
Q_ENUMS(myenum)
public:
MyQtAppEnums (void) {};

#endif

myenum {
the = (1<<8),
number,
is,
fortytwo = 42
};

#ifdef QT
}
#endif

The above might not be 100%, but shows at least how a c style header can be kept intact for a c-compiler and make the enums a bit more qt-friendly when using moc + a c++ compiler.


thanks,
leif