Q_ENUM and QVariant in a class in a namespace
-
I have a class with the structure:
namespace NS { //Q_NAMESPACE // Apparently I can't have multiple Q_NAMESPACE definitions if multiple headers have the same namespace declaration class A : public QObject { Q_OBJECT public: enum class MyEnum:quint8 {A,B,C}; Q_ENUM(NS::A::MyEnum) }; }
I then want to create a QVariant from this enum:
void SomeFunc() { QVariant V(NS::A::MyEnum::A); }
... this fails, stating that there is no valid conversion from the enum to QVariant. Am I doing something wrong with the Q_ENUM macro above? I'm having difficulty deciphering when I should use Q_NAMESPACE, Q_ENUM_NS, Q_ENUM, etc. Apparently if an enum is in a class, you don't use Q_ENUM_NS even if the class is in a namespace. Any pointers?
-
According to the docs it must be Q_ENUM(MyEnum). And I'm not quite sure it is working with
enum class
-
@Christian-Ehrlicher if memory serves well, enum class should already work.
-
It works, I am using it every day!
-
Can you provide a minimal project that triggers your issue ?
That will allow for everybody to work on the same base. -
Sure, just add the code below to the post above (using the class in A.h and filling in a basic main() function for SomFunc() for a test project:
CMakeLists.txt:
cmake_minimum_required(VERSION 3.5) project(project LANGUAGES CXX) set(CMAKE_AUTOMOC ON) find_package(Qt6 COMPONENTS Core REQUIRED) set(sources main.cpp) set(headers A.h) add_executable(test ${sources} ${headers}) target_link_libraries(test PRIVATE Qt6::Core)