PDA

View Full Version : QMetaType::type() returning 0 with Q_DECLARE_METATYPE class ?



epixerion
11th March 2017, 15:51
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 :


//This Work
auto metaEnum = QMetaEnum::fromType<FRShapeNode::FRSHAPE_TYPE>();
EASYPRINT("%s yo", metaEnum.valueToKey(FRShapeNode::FRSHAPE_TYPE::CV_ CIRCLE))

//This Dont (QMetaType::type("FRSHapeNode") return 0)
int classId = QMetaType::type("FRShapeNode");
if (classId != QMetaType::UnknownType) {
auto metaObject = QMetaType::metaObjectForType(classId);
int enumId = metaObject->indexOfEnumerator("FRSHAPE_TYPE");
QMetaEnum e = metaObject->enumerator(enumId);
for (int i = 0; i < e.keyCount(); i++)
{
const char* s = e.key(i); // enum name as string
int v = e.value(i); // enum index
appendToResult(MString(s) + "=" + v);
}
}

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


#pragma once

#include "FRDagNode.h"
#include <QObject>

class FRShapeNode : public FRDagNode
{
Q_GADGET
public:
FRShapeNode();
FRShapeNode(const FRShapeNode &other) { }
~FRShapeNode();

static MTypeId id;

enum FRSHAPE_TYPE
{
CV_CIRCLE = 0,
CV_LINE,
CV_CUSTOM
};
Q_ENUM(FRSHAPE_TYPE)

protected:
typedef FRDagNode super;
};

Q_DECLARE_METATYPE(FRShapeNode)

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.

high_flyer
11th March 2017, 23:50
What happens if you use qMetaTypeId (http://doc.qt.io/qt-5/qmetatype.html#qMetaTypeId)

anda_skoa
12th March 2017, 13:05
Didi you register the type with qRegisterMetaType()?

Cheers,
_

epixerion
12th March 2017, 14:57
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 !

anda_skoa
13th March 2017, 08:46
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,
_

epixerion
13th March 2017, 12:51
Thanks for clarifying that out !

Have a good day.