Im trying to do the same thing that trolltech does in qnamespace.h. When moc runs it's an object but when the compiler runs it's a namespace. I think this will enable me to use staticMetaObject and QMetaEnum to get my enums as strings also.

I am aware that i can do this in my class if i inherit QObject, but i want my enums in a namespace. Just like Qt

Here's what im trying:
Qt Code:
  1. #include <QtCore/QObject>
  2.  
  3. #ifndef Q_MOC_RUN
  4. namespace
  5. #else
  6. class ICOMM_EXPORT
  7. #endif
  8. iComm
  9. /*
  10. #if defined(Q_MOC_RUN)
  11. :public QObject
  12. #endif
  13. */
  14. {
  15. #if defined(Q_MOC_RUN)
  16. Q_OBJECT
  17. Q_ENUMS(PackageType)
  18. #endif
  19.  
  20. #if defined(Q_MOC_RUN)
  21. public:
  22. #endif
  23. enum PackageType{
  24. Unknown,
  25. Ping,
  26. Pong,
  27. SendUserInfo,
  28. UserInfo,
  29. SendAgentInfo,
  30. AgentInfo,
  31. AddUser,
  32. UpdateStatus,
  33. SendStatusInfo,
  34. StatusInfo,
  35. SendOnlineStatus,
  36. OnlineStatus,
  37. IM
  38. };
  39. }
  40. #ifdef Q_MOC_RUN
  41. ;
  42. #endif
To copy to clipboard, switch view to plain text mode 

But moc complains:

src/iNamespace.hh:17: Error: Class contains Q_OBJECT macro but does not inherit from QObject

If i make it inherit QObject g++ complains:

src/.moc/moc_iNamespace.cpp:61: error: ‘iComm::staticMetaObject’ should have been declared inside ‘iComm’
src/.moc/moc_iNamespace.cpp:66: error: ‘const QMetaObject* iComm::metaObject()’ should have been declared inside ‘iComm’
src/.moc/moc_iNamespace.cpp:66: error: non-member function ‘const QMetaObject* iComm::metaObject()’ cannot have cv-qualifier
src/.moc/moc_iNamespace.cpp:71: error: ‘void* iComm::qt_metacast(const char*)’ should have been declared inside ‘iComm’
src/.moc/moc_iNamespace.cpp: In function ‘void* iComm::qt_metacast(const char*)’:
src/.moc/moc_iNamespace.cpp:75: error: expected type-specifier before ‘iComm’
src/.moc/moc_iNamespace.cpp:75: error: expected `>' before ‘iComm’
src/.moc/moc_iNamespace.cpp:75: error: expected `(' before ‘iComm’
src/.moc/moc_iNamespace.cpp:75: error: expected primary-expression before ‘*’ token
src/.moc/moc_iNamespace.cpp:75: error: expected primary-expression before ‘>’ token
src/.moc/moc_iNamespace.cpp:75: error: invalid use of ‘this’ in non-member function
src/.moc/moc_iNamespace.cpp:75: error: expected `)' before ‘;’ token
src/.moc/moc_iNamespace.cpp:76: error: cannot call member function ‘virtual void* QObject::qt_metacast(const char*)’ without object
src/.moc/moc_iNamespace.cpp: At global scope:
src/.moc/moc_iNamespace.cpp:79: error: ‘int iComm::qt_metacall(QMetaObject::Call, int, void**)’ should have been declared inside ‘iComm’
src/.moc/moc_iNamespace.cpp: In function ‘int iComm::qt_metacall(QMetaObject::Call, int, void**)’:
src/.moc/moc_iNamespace.cpp:81: error: cannot call member function ‘virtual int QObject::qt_metacall(QMetaObject::Call, int, void**)’ without object
make: *** [src/.obj/moc_iNamespace.o] Error 1