Emitting enum class in namespace not working
-
Code: https://github.com/Drllap/EmitEnumFromNamspaceTest
Looks like I was wrong about this working with structs:
MyEnum
andMyStruct
are metatype declared with:namespace MyNamespace { Q_DECLARE_METATYPE(MyEnum) Q_DECLARE_METATYPE(MyStruct) }
and are successfully sent between threads but
MyEnum2
,MyStruct2
Q_DECLARE_METATYPE(MyNamespace::MyEnum2) Q_DECLARE_METATYPE(MyNamespace::MyStruct2)
are not sent between threads.
Then there is another problem, this:
namespace MyNamespace { Q_DECLARE_METATYPE(MyEnum) Q_DECLARE_METATYPE(MyStruct) }
doesn't compile on Ubuntu18 with GCC
-
@Palli said in Emitting enum class in namespace not working:
are not sent between threads.
They are, please recheck.
doesn't compile on Ubuntu18 with GCC
Because you use the macro wrong:
If MyStruct is in a namespace, the Q_DECLARE_METATYPE() macro has to be outside the namespace.
This works as expected (and shown in the documentation)
Q_DECLARE_METATYPE(MyNamespace::MyEnum) Q_DECLARE_METATYPE(MyNamespace::MyEnum2) Q_DECLARE_METATYPE(MyNamespace::MyStruct) Q_DECLARE_METATYPE(MyNamespace::MyStruct2) void MetaRegister::RegisterAllOfTheThings() { qDebug() << "Registering meta type: " << qRegisterMetaType<MyNamespace::MyEnum>(); qDebug() << "Registering meta type: " << qRegisterMetaType<MyNamespace::MyEnum2>(); qDebug() << "Registering meta type: " << qRegisterMetaType<MyNamespace::MyStruct>(); qDebug() << "Registering meta type: " << qRegisterMetaType<MyNamespace::MyStruct2>(); }
-
They are, please recheck.
I did, and they are not.
Because you use the macro wrong:
I know I'm using the macro wrong, but it is the only way I have found to get the signaling between threads to work.
Q_DECLARE_METATYPE(MyNamespace::MyEnum)
Q_DECLARE_METATYPE(MyNamespace::MyEnum2)
Q_DECLARE_METATYPE(MyNamespace::MyStruct)
Q_DECLARE_METATYPE(MyNamespace::MyStruct2)void MetaRegister::RegisterAllOfTheThings()
{
qDebug() << "Registering meta type: " << qRegisterMetaTypeMyNamespace::MyEnum();
qDebug() << "Registering meta type: " << qRegisterMetaTypeMyNamespace::MyEnum2();
qDebug() << "Registering meta type: " << qRegisterMetaTypeMyNamespace::MyStruct();
qDebug() << "Registering meta type: " << qRegisterMetaTypeMyNamespace::MyStruct2();
}If I use this code above, I get this:
-
Please push the code with my changes so others can test it
-
@Christian-Ehrlicher Done
-
Ah, you still forget to fully qualify the enums in the slots as I said in my first post. I did this locally.
-
I don't quite follow, are you talking about in the Receiver class?
class Receiver : public QObject { Q_OBJECT public slots: void OnGettingEnum(MyNamespace::MyEnum e) { qDebug() << "Received on thread: " << QThread::currentThread(); qDebug() << "Got the MyEnum: " << static_cast<int>(e); } void OnGettingEnum2(MyNamespace::MyEnum2 e) { qDebug() << "Received on thread: " << QThread::currentThread(); qDebug() << "Got the MyEnum2: " << static_cast<int>(e); } void OnGettingStruct(MyNamespace::MyStruct s) { qDebug() << "Received on thread: " << QThread::currentThread(); qDebug() << "Got the MyStruct"; } void OnGettingStruct2(MyNamespace::MyStruct2 s) { qDebug() << "Received on thread: " << QThread::currentThread(); qDebug() << "Got the MyStruct2"; } };
-
Ok, you are talking about the signals in the Sender class?
Changing this
class Sender : public QObject { Q_OBJECT signals: void EmitEnum(MyEnum); void EmitEnum2(MyEnum2); void EmitStruct(MyStruct); void EmitStruct2(MyStruct2); };
to this
class Sender : public QObject { Q_OBJECT signals: void EmitEnum(MyNamespace::MyEnum); void EmitEnum2(MyNamespace::MyEnum2); void EmitStruct(MyNamespace::MyStruct); void EmitStruct2(MyNamespace::MyStruct2); };
fixes it.
-
@Palli said in Emitting enum class in namespace not working:
Ok, you are talking about the signals in the Sender class?
Yes:
forgot to add the namespace to your signal - otherwise there would have been a warning about my_namespace ::my_enum and not my_enum.
Please mark the topic as solved then.
-
@Christian-Ehrlicher Done, thanks for you help