Results 1 to 6 of 6

Thread: QMetaType::type() returning 0 with Q_DECLARE_METATYPE class ?

  1. #1
    Join Date
    Mar 2017
    Posts
    3
    Qt products
    Qt5

    Default QMetaType::type() returning 0 with Q_DECLARE_METATYPE class ?

    Hi,

    I'm in need of using the reflection power of QT, so I recently implemented Qt5.6.1 on my project, I would like to be able to retrieve any data inside my object using string, but i can't get the Q_Declare_MetaType() to work, but i can still get metadata inside my class

    eg :

    Qt Code:
    1. //This Work
    2. auto metaEnum = QMetaEnum::fromType<FRShapeNode::FRSHAPE_TYPE>();
    3. EASYPRINT("%s yo", metaEnum.valueToKey(FRShapeNode::FRSHAPE_TYPE::CV_CIRCLE))
    4.  
    5. //This Dont (QMetaType::type("FRSHapeNode") return 0)
    6. int classId = QMetaType::type("FRShapeNode");
    7. if (classId != QMetaType::UnknownType) {
    8. auto metaObject = QMetaType::metaObjectForType(classId);
    9. int enumId = metaObject->indexOfEnumerator("FRSHAPE_TYPE");
    10. QMetaEnum e = metaObject->enumerator(enumId);
    11. for (int i = 0; i < e.keyCount(); i++)
    12. {
    13. const char* s = e.key(i); // enum name as string
    14. int v = e.value(i); // enum index
    15. appendToResult(MString(s) + "=" + v);
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    here is my .hpp of the class i'm trying to register :

    Qt Code:
    1. #pragma once
    2.  
    3. #include "FRDagNode.h"
    4. #include <QObject>
    5.  
    6. class FRShapeNode : public FRDagNode
    7. {
    8. Q_GADGET
    9. public:
    10. FRShapeNode();
    11. FRShapeNode(const FRShapeNode &other) { }
    12. ~FRShapeNode();
    13.  
    14. static MTypeId id;
    15.  
    16. enum FRSHAPE_TYPE
    17. {
    18. CV_CIRCLE = 0,
    19. CV_LINE,
    20. CV_CUSTOM
    21. };
    22. Q_ENUM(FRSHAPE_TYPE)
    23.  
    24. protected:
    25. typedef FRDagNode super;
    26. };
    27.  
    28. Q_DECLARE_METATYPE(FRShapeNode)
    To copy to clipboard, switch view to plain text mode 
    I'm pretty new with Qt c++, I was mostly using Pyside for month.

    I'm Using Qt5.6.1 and compiling my project as a dynamic Library, which is then loaded inside Maya 2017.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QMetaType::type() returning 0 with Q_DECLARE_METATYPE class ?

    What happens if you use qMetaTypeId
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMetaType::type() returning 0 with Q_DECLARE_METATYPE class ?

    Didi you register the type with qRegisterMetaType()?

    Cheers,
    _

  4. #4
    Join Date
    Mar 2017
    Posts
    3
    Qt products
    Qt5

    Default Re: QMetaType::type() returning 0 with Q_DECLARE_METATYPE class ?

    Hi, I just tried to add the qRegisterMetaType() and its work now, thanks for that anda_skoa,

    but I don't really understand why we have to use some c++ code to register the type, wasn't the MACRO here for that ? its not really specified in the doc that you must call qRegisterMetaType alongside the macro for it to work, what is the point ?

    Thanks for the help !

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMetaType::type() returning 0 with Q_DECLARE_METATYPE class ?

    The macro "declares" the meta type, the function "instantiates" the meta type information.

    Whether the latter is necessary depends on how the meta type is being used.

    For example declaring is good enough if you use the meta type with QVariant::fromValue(), as that will take care of registring on first usage.

    Other use cases, like the type being used as a signal/slot argument in a Qt::QueuedConnection do require explicit registration as no code is called that does it implicitly.

    That's also true for your case since you never use your class as a template argument with any of the Qt functions.

    Cheers,
    _

  6. #6
    Join Date
    Mar 2017
    Posts
    3
    Qt products
    Qt5

    Default Re: QMetaType::type() returning 0 with Q_DECLARE_METATYPE class ?

    Thanks for clarifying that out !

    Have a good day.

Similar Threads

  1. Replies: 3
    Last Post: 25th June 2015, 10:11
  2. Replies: 5
    Last Post: 22nd February 2013, 13:11
  3. Replies: 2
    Last Post: 11th July 2012, 00:18
  4. Replies: 11
    Last Post: 24th November 2009, 00:30
  5. QVariant not returning proper type for SQLite REAL
    By rakkar in forum Qt Programming
    Replies: 1
    Last Post: 15th September 2009, 20:25

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.