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
    Join Date
    Mar 2009
    Posts
    14
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1

    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

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

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

    Use Q_GADGET instead of Q_OBJECT.
    J-P Nurmi

  3. #3

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

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

    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

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

    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

  6. #6

    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!

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

    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

  8. #8

    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!

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

    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

  10. #10
    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, 18:48
  2. Qt4 : QPainter::setRedirected doesn't work
    By Ankitha Varsha in forum Qt Programming
    Replies: 2
    Last Post: 20th June 2008, 18:52
  3. QActions don't work with menubar hidden
    By Pepe in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2007, 02:04
  4. Change work area OS
    By pakulo in forum Qt Programming
    Replies: 15
    Last Post: 15th May 2007, 08:20
  5. Replies: 4
    Last Post: 7th March 2007, 10: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.