Register Qt type as meta type
-
Can Qt types can be register in meta object system? I tried to register enum QtMsgType in class inherited from QObject with qobject macro and qenum macro and in class with qgadget. *QMetaEnum::enumName() returns nullptr in both cases but compiler doesnt say something about what i do wrong. QMetaObject::indexOfEnumerator returns -1 but i register type.
class Logging { Q_GADGET Q_ENUM(QtMsgType); ... public: void static messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg); // all other methods are static }
void Logging::messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) { ... auto metaEnum = QMetaEnum::fromType<QtMsgType>; auto value = metaEnum().enumName(); // nullptr auto variant = QVariant::fromValue(type); //null variant ... }
-
If your enum is contained in your class - use
Q_ENUM
and that's enough to declare it as metatype. Otherwise useQ_DECLARE_METATYPE
as @gde23 said. -
@kshegunov i tried this too, it didnt work. I think your missed what my enum really is. It's not mine enum, its Qt enum.
-
@PaulNewman said in Register Qt type as meta type:
I think your missed what my enum really is. It's not mine enum, its Qt enum.
You're very much correct.
Q_DECLARE_METATYPE
should be the thing to go to in this case, but I'm curious, why do you want this as a metatype?PS. You may want also to call
qRegisterMetaType<QtMsgType>()
to get the type registered, in addition to being declared.