How to use enum declared in another class as Q_Enum
-
@jsulm said in How to use enum declared in another class as Q_Enum:
From design point of view it is better do declare enums where they belong to instead of putting all eneums into one place.
I beg to differ. Classes with included enums are almost impossible to forward-declare. So if you have a large project and want to use forward declarations extensively, you are better off having enums in separate header files.
-
@IknowQT said in How to use enum declared in another class as Q_Enum:
If there is an enum class that is commonly used, it would be more efficient to use it in one place.
So can you answer my question?
Is there any way to fix the error?Have you looked at Q_ENUM_NS?
-
@Asperamanca said in How to use enum declared in another class as Q_Enum:
Q_ENUM_NS
I tried it, but the same error occurs.
Can you give me one example?
-
@IknowQT
Hi,
The second answer in this link works well to mehttps://stackoverflow.com/questions/20089196/how-to-access-c-enum-from-qml
Only issue, the qml editor doesn’t show the tips when start tip text ( Ex MyNamespace. after point the editor doesn’t show the possible keys, I’m working on
-
-
If you look closely at the compiler errors, you'll notice that the complaints are directed toward the namespace, not the enumeration. The namespace does not have a static QMetaObject to which the Qt MetaObject system can relate the enumeration. To correct this, you must enable the namespace to participate in the Qt MetaObject system[1]. For example:
namespace CollectEnums { Q_NAMESPACE enum class CELL_TYPE { Single = 0, Multi, Nanoliter }; Q_ENUM_NS(CELL_TYPE) }
The net effect of adding 'Q_NAMESPACE' to a namespace is similar to adding 'Q_OBJECT' in that it creates a static MetaObject for the type.
After doing this, you can refer to 'CollectEnums::CELL_TYPE' as a type and 'CollectEnums::CELL_TYPE::Single' as a value for that type.
Also, in my experience, using 'enum class' (instead of just plain 'enum') also requires that the type be registered with the QMetaType system if the type is going to be used in a QVariant. This is due to how plain enums are treated (like a synonym for 'int'), and 'enum class' (which is defining a new type) [2]. To do this, add this line in the same header file, but outside of the namespace declaration:
namespace CollectEnums { Q_NAMESPACE . . . } Q_DECLARE_METATYPE(CollectEnums::CELL_TYPE)
Doing this is absolutely necessary if the type will be referred to in QML.
Cheers.
-
@jadelisle said in How to use enum declared in another class as Q_Enum:
If you look closely at the compiler errors, you'll notice that the complaints are directed toward the namespace, not the enumeration. The namespace does not have a static QMetaObject to which the Qt MetaObject system can relate the enumeration. To correct this, you must enable the namespace to participate in the Qt MetaObject system[1]. For example:
error LNK2001 "struct QMetaObject const CollectEnums::staticMetaObject" (?staticMetaObject@CollectEnums@@3UQMetaObject@@B)
I tried it, but the same error occurs.
For reference, we are developing in the QWidget method in the vs2019 environment.namespace CollectEnums { Q_NAMESPACE enum class CELL_TYPE { Single = 0, Multi, Nanoliter, Sipper, Temperature, Max }; Q_ENUM_NS(CELL_TYPE); } Q_DECLARE_METATYPE(CollectEnums::CELL_TYPE) QString cModelCommon::EnumtoStringCellType(const CollectEnums::CELL_TYPE e) { return QVariant::fromValue(e).toString(); } CollectEnums::CELL_TYPE cModelCommon::StringtoEnumCellType(QString str) { CollectEnums::CELL_TYPE e = CollectEnums::CELL_TYPE::Single; QMetaEnum map = QMetaEnum::fromType<CollectEnums::CELL_TYPE>(); for (qint32 i = (qint32)CollectEnums::CELL_TYPE::Single; i < map.keyCount(); i++) { if (map.key(i) == str) { e = CollectEnums::CELL_TYPE(i); break; } } return e; }
The peculiar thing is that if you comment out the code inside the EnumtoStringCellType and StringtoEnumCellType functions, it will be built.
I want to convert an enum to a string and vice versa, convert a string to an enum. -
I tested it, but the result is the same error.
It doesn't seem to be a problem with the code, but it seems to be because of the structure of my source code.
cModelCommon inherits cSerializer as its parent
If you look at the structure of cSerializer, there are xml parsing codes.
It was used by declaring Q_GADGET and QS_SERIALIZABLE instead of qobject.