Results 1 to 12 of 12

Thread: Namespace,qobject,enum trick. How to get it to work?

  1. #1
    Join Date
    Mar 2009
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Namespace,qobject,enum trick. How to get it to work?

    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

  2. #2
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Namespace,qobject,enum trick. How to get it to work?

    Like it says, you aren't inheriting from QObject. So how you expect it to work?
    Qt-4.7.3 | Gentoo ~amd64 | KDE-4.7
    Aki IRC Client for KDE4 | Qt Documentation

  3. #3
    Join Date
    Mar 2009
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Namespace,qobject,enum trick. How to get it 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

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Namespace,qobject,enum trick. How to get it to work?

    Use Q_GADGET instead of Q_OBJECT.
    J-P Nurmi

  5. #5

    Default Re: Namespace,qobject,enum trick. How to get it to work?

    Quote Originally Posted by jpn View Post
    Use Q_GADGET instead of Q_OBJECT.
    This doesn't seem to work, inheriting from QObject or not ...

    Still complains about missing staticMetaObject...

  6. #6
    Join Date
    Mar 2009
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Namespace,qobject,enum trick. How to get it to work?

    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

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Namespace,qobject,enum trick. How to get it to work?

    Quote Originally Posted by SnarlCat View Post
    This doesn't seem to work, inheriting from QObject or not ...

    Still complains about missing staticMetaObject...
    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

  8. #8

    Default Re: Namespace,qobject,enum trick. How to get it to work?

    Ok.. so time for a simplistic example:
    Qt Code:
    1. // FILENAME: foo.h
    2. #ifndef FOO_H
    3. #define FOO_H
    4.  
    5. #include <QObject>
    6. #ifdef Q_MOC_RUN
    7. class foo {
    8. Q_GADGET
    9. Q_ENUMS(Direction)
    10. public:
    11. enum Direction { Up, Down, Left, Right, Front, Back };
    12. };
    13. #else
    14. namespace foo {
    15. enum Direction { Up, Down, Left, Right, Front, Back };
    16. }
    17. #endif
    18.  
    19. #endif // FOO_H
    20.  
    21. //FILENAME: bar.h
    22.  
    23. #include <QtCore>
    24. #include "foo.h"
    25.  
    26. class bar: public QObject {
    27. Q_OBJECT
    28. public:
    29. bar();
    30. foo::Direction getDirection() { return _dir; }
    31. private:
    32. foo::Direction _dir;
    33. };
    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!

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Namespace,qobject,enum trick. How to get it to work?

    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

  10. #10

    Default Re: Namespace,qobject,enum trick. How to get it to work?

    So you think something like:
    Qt Code:
    1. #ifdef Q_MOC_RUN
    2. class foo {
    3. Q_GADGET
    4. Q_ENUMS(Direction)
    5. public:
    6. enum Direction { Up, Down, Left, Right, Front, Back };
    7. };
    8. #else
    9. namespace foo {
    10. Q_ENUMS(Direction)
    11. enum Direction { Up, Down, Left, Right, Front, Back };
    12. }
    13. #endif
    To copy to clipboard, switch view to plain text mode 

    ...is what we're looking for?

    This certainly wasn't evident in qnamespace.h ...

    Thanks!

  11. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Namespace,qobject,enum trick. How to get it to work?

    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

  12. #12
    Join Date
    Feb 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Namespace,qobject,enum trick. How to get it to work?

    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:
    1. if (def.classname != "Qt" && def.classname != "QObject" && def.superclassList.isEmpty())
    2. error("Class contains Q_OBJECT macro but does not inherit from QObject");
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. [thread]how to suspend a work thread
    By viasant in forum Qt Programming
    Replies: 2
    Last Post: 8th April 2013, 17:48
  2. Qt4 : QPainter::setRedirected doesn't work
    By Ankitha Varsha in forum Qt Programming
    Replies: 2
    Last Post: 20th June 2008, 17:52
  3. QActions don't work with menubar hidden
    By Pepe in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2007, 01:04
  4. Change work area OS
    By pakulo in forum Qt Programming
    Replies: 15
    Last Post: 15th May 2007, 07:20
  5. Replies: 4
    Last Post: 7th March 2007, 09:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.