Results 1 to 12 of 12

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    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!

  2. #2
    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

  3. #3

    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!

  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?

    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

  5. #5
    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.