AndresBarbaRoja
24th March 2011, 10:16
Hi, I am recieving a string that defines the function i have to call. To do this I wrote this class:
class MiTableClass:public QObject
{
Q_OBJECT
Q_ENUMS(MiTableElements);
//This is a singleton class but i deleted the extra code lines for this post.
public:
int indexOf(QString);
QString commandNumber(int);
enum MiTableElements
{
//Eth commands
AUTOTEST,//0
HALT,//1
STANDBY,//2
SSNORMAL,//3
};//The enum is way longer
};
int MiTableClass::indexOf(QString command)
{
const QMetaObject metaObject=m_Instance->staticMetaObject;
int index = metaObject.indexOfEnumerator("MiTableElements");
QMetaEnum metaEnum = metaObject.enumerator(index);
int i=metaEnum.keyToValue(command.toLatin1());
return i;
}
QString MiTableClass::commandNumber(int nCmd)
{
const QMetaObject metaObject=m_Instance->staticMetaObject;
int index = metaObject.indexOfEnumerator("MiTableElements");
QMetaEnum metaEnum = metaObject.enumerator(index);
QString str=metaEnum.valueToKey(nCmd);
return str;
}
and the to use it I do this
void HLP_MessageProcessor::run()
{
MiTableClass * mi =MiTableClass::Instance();
lst=message.split(",");
//Figuring out the command code
int i=mi->indexOf(lst[0]);
switch(i)
{
case 0://AUTOTEST
AUTOTEST_Function();
break;
case 1://HALT
HALT_Function();
//The lack of break here is on purpouse
case 2://STANDBY
STANDBY_Function();
}
}
Now the thing is that I will have to add cases inbetween the ones i already have and I would like to be able to write
switch(i)
{
case AUTOTEST:
...
And i do not know where to declare the enum so I can use it in this way for the switch.
So far i tried to put it as a global enum, but then the class members that compare the string with the enum value stopped to work.
Any ideas?
How to register the global enum inside my class? i'm lost in this topic
class MiTableClass:public QObject
{
Q_OBJECT
Q_ENUMS(MiTableElements);
//This is a singleton class but i deleted the extra code lines for this post.
public:
int indexOf(QString);
QString commandNumber(int);
enum MiTableElements
{
//Eth commands
AUTOTEST,//0
HALT,//1
STANDBY,//2
SSNORMAL,//3
};//The enum is way longer
};
int MiTableClass::indexOf(QString command)
{
const QMetaObject metaObject=m_Instance->staticMetaObject;
int index = metaObject.indexOfEnumerator("MiTableElements");
QMetaEnum metaEnum = metaObject.enumerator(index);
int i=metaEnum.keyToValue(command.toLatin1());
return i;
}
QString MiTableClass::commandNumber(int nCmd)
{
const QMetaObject metaObject=m_Instance->staticMetaObject;
int index = metaObject.indexOfEnumerator("MiTableElements");
QMetaEnum metaEnum = metaObject.enumerator(index);
QString str=metaEnum.valueToKey(nCmd);
return str;
}
and the to use it I do this
void HLP_MessageProcessor::run()
{
MiTableClass * mi =MiTableClass::Instance();
lst=message.split(",");
//Figuring out the command code
int i=mi->indexOf(lst[0]);
switch(i)
{
case 0://AUTOTEST
AUTOTEST_Function();
break;
case 1://HALT
HALT_Function();
//The lack of break here is on purpouse
case 2://STANDBY
STANDBY_Function();
}
}
Now the thing is that I will have to add cases inbetween the ones i already have and I would like to be able to write
switch(i)
{
case AUTOTEST:
...
And i do not know where to declare the enum so I can use it in this way for the switch.
So far i tried to put it as a global enum, but then the class members that compare the string with the enum value stopped to work.
Any ideas?
How to register the global enum inside my class? i'm lost in this topic