PDA

View Full Version : converting strings into enums



ugluk
26th August 2011, 08:58
I'm using the code below to convert strings into the BackgroundFlag enum. The problem is, that I cannot have repeat-x, repeat-y or repeat-xy as enums, due to the minus - sign. Is there a workaround possible?



class BackgroundUtility : public QObject
{
Q_OBJECT

Q_ENUMS(BackgroundFlag)

Q_PROPERTY(enum BackgroundFlag backgroundFlag READ backgroundFlag WRITE setBackgroundFlag)

public:
enum BackgroundFlag
{
none = 0,
reset = 0,
repeatx,
repeaty,
repeatxy
};

enum BackgroundFlag backgroundFlag() const { return flag; }
void setBackgroundFlag(enum BackgroundFlag f) { flag = f; }

private:
enum BackgroundFlag flag;
};

gkarthick5
26th August 2011, 09:02
You can use _ (underscore). AFAIK, only valid C++ variable names can be used as enum constants

ugluk
26th August 2011, 09:19
Sure, I could use the underscore _, but I want to use the minus -.

wysota
26th August 2011, 09:35
Then find a different mechanism.

ugluk
26th August 2011, 09:54
Can you give a suggestion? I like to use the Qt generator mechanism, because I like to avoid all those qstrcmps.

wysota
26th August 2011, 10:59
Those comparisons would be there anyway, just hidden behind autogenerated code. If you really want to use this approach then use underscore as suggested and just replace all hypens from the userland with underscores on the fly when accessing the enum metadata.