XML, read file, string to enum.....
Hello everybody,
i have an xml file which i read during start-up and write when the programm is quit.
The dataset is read into a qmap during parsing. Two parameters i read through the process are form typ enum.
Code:
///*! Enum for xxx names*/
typedef enum {
x1 = 0, /***/
x3, /***/
x8, /***/
undefined_xxNames /***/
}EnumxxNames;
QString getEnumxxNames
(EnumxxNames element
);
Code:
QString graphicsEnum
::getEnumxxNames(EnumxxNames element
) {
switch(element)
{
case x1:
case x3:
case x8:
default:
}
}
in xml parser
Code:
qDebug()<<readItem(xx,getStringXMLItem(Properties),getStringXMLItemProperties(xxName));
Note readItem gives back a Qstring -->
reads out x1, x3,x8
if i want to conert that back to the enum using static cast
Code:
qDebug()<<getEnumxxNames(static_cast<EnumxxNames>(readItem(xx,getStringXMLItem(Properties),getStringXMLItemProperties(xxName)));
reads out x8
why is that...what is wrong here....
Is there a other way to get this done ?
Re: XML, read file, string to enum.....
You cannot cast a string to an integer or enum.
Just like for the conversion in the other direction you'll need a function that understands the enum and its string representation.
Something like
Code:
EnumxxNames getEnumValue
(const QString &element
){
if (element == "x1") return x1;
if (element == "x3") return x3;
return static_cast<EnumxxNames>(element.toInt()); // actually should check the result of toInt before casting
}
Alternatively you can do the string -> value mapping with a map or hash that uses the string representation as the keys and the respective enum value as the values.
Cheers,
_