As my post says, i have tried it as well with the results posted above. And in <qtsource>/src/corelib/global/qnamespace.h the troll do not inherit the QObject. I would really like to get this to work![]()
As my post says, i have tried it as well with the results posted above. And in <qtsource>/src/corelib/global/qnamespace.h the troll do not inherit the QObject. I would really like to get this to work![]()
Use Q_GADGET instead of Q_OBJECT.
J-P Nurmi
I've given up. I've tried everything i can think of. I have examined the preprocessor output with MOC_RUN defined and without. In qtcore qnamespace.h compiles fine as a namespace with a metaobject. But i cant reproduce it in my project. So i, as a second choise, used Q_GADGET in my class. This at least enables me to use QMetaEnum with an object which does not inherit from QObject. It would have been cooler with a namespace. But i will supress my compulsions, and go on with my life![]()
First of all, Q_GADGET is meant for non-QObjects. Just don't forget to re-run qmake after adding or removing Q_GADGET. Just like you would do with Q_OBJECT. Furthermore, you need to include the moc file by hand if you have Q_GADGET or Q_OBJECT in a .cpp file (it should be in a header file to avoid that).
J-P Nurmi
Ok.. so time for a simplistic example:
Qt Code:
// FILENAME: foo.h #ifndef FOO_H #define FOO_H #include <QObject> #ifdef Q_MOC_RUN class foo { Q_GADGET Q_ENUMS(Direction) public: enum Direction { Up, Down, Left, Right, Front, Back }; }; #else namespace foo { enum Direction { Up, Down, Left, Right, Front, Back }; } #endif #endif // FOO_H //FILENAME: bar.h #include <QtCore> #include "foo.h" Q_OBJECT public: bar(); foo::Direction getDirection() { return _dir; } private: foo::Direction _dir; };To copy to clipboard, switch view to plain text mode
Moc seems to work (how would I know if it didn't?), but classes that include the header the above block is in, complain that "'staticMetaObject' is not a member of 'foo'" or "'staticMetaObject': undeclared identifier"
Thoughts on getting this working?
Thanks!
It's the macro which defines the member. In your code only moc can see the macro. You define a plain name space otherwise.
J-P Nurmi
So you think something like:
Qt Code:
#ifdef Q_MOC_RUN class foo { Q_GADGET Q_ENUMS(Direction) public: enum Direction { Up, Down, Left, Right, Front, Back }; }; #else namespace foo { Q_ENUMS(Direction) enum Direction { Up, Down, Left, Right, Front, Back }; } #endifTo copy to clipboard, switch view to plain text mode
...is what we're looking for?
This certainly wasn't evident in qnamespace.h ...
Thanks!
No, I meant just using a class. If you want to, you can fake it to be a namespace for doxygen, though.
J-P Nurmi
Apologies for the necro-posting but since this is one of the top google hits:
http://comments.gmane.org/gmane.comp.lib.qt.user/5256
>> The real point is: is there some 'magic' qmake flag or something else
>> used in qnamespace.h for having
>> that working properly ?
>
> Yes, there's magic hardcoded in moc and in QMetaObject:roperty.
src/tools/moc/moc.cpp:653:
Qt Code:
if (def.classname != "Qt" && def.classname != "QObject" && def.superclassList.isEmpty()) error("Class contains Q_OBJECT macro but does not inherit from QObject");To copy to clipboard, switch view to plain text mode
Bookmarks