ENUM value from QString
-
Hi Everyone
I have a lot of custom class with enum define like this:
class BillingEvents : public QObject { Q_OBJECT Q_ENUMS(EnumBillingEvents) public: explicit BillingEvents(QObject *parent = 0); enum EnumBillingEvents { APP_INSTALLS, CLICKS, IMPRESSIONS, LINK_CLICKS, MULTI_PREMIUM, OFFER_CLAIMS, PAGE_LIKES, POST_ENGAGEMENT, VIDEO_VIEWS }; }; Q_DECLARE_METATYPE(BillingEvents*);
What I wan't is convert QString something like "APP_INSTALLS", "LINK_CLICK" to Enum Value.
I'm tried this but it doesn't work
const char *chr = "BillingEvents"; int type = QMetaType::type(chr); const QMetaObject* metaObj = QMetaType::metaObjectForType(type); int index = metaObj->indexOfEnumerator("EnumBillingEvents"); QMetaEnum metaEnum = metaObj->enumerator(index); const char* key = "LINK_CLICKS"; int enumVal = metaEnum.keyToValue(key); val = QVariant::fromValue(enumVal);
First Question: Why I cannot register type? I have to add * after Class Name to pass this Error
Q_DECLARE_METATYPE(BillingEvents); //error: 'QObject::QObject(const QObject&)' is private //Q_DISABLE_COPY(QObject)
Second Question: How can I get type id (int) from type name? The following code doesn't work.
const char *chr = "BillingEvents"; int type = QMetaType::type(chr);
-
Hi Everyone
I have a lot of custom class with enum define like this:
class BillingEvents : public QObject { Q_OBJECT Q_ENUMS(EnumBillingEvents) public: explicit BillingEvents(QObject *parent = 0); enum EnumBillingEvents { APP_INSTALLS, CLICKS, IMPRESSIONS, LINK_CLICKS, MULTI_PREMIUM, OFFER_CLAIMS, PAGE_LIKES, POST_ENGAGEMENT, VIDEO_VIEWS }; }; Q_DECLARE_METATYPE(BillingEvents*);
What I wan't is convert QString something like "APP_INSTALLS", "LINK_CLICK" to Enum Value.
I'm tried this but it doesn't work
const char *chr = "BillingEvents"; int type = QMetaType::type(chr); const QMetaObject* metaObj = QMetaType::metaObjectForType(type); int index = metaObj->indexOfEnumerator("EnumBillingEvents"); QMetaEnum metaEnum = metaObj->enumerator(index); const char* key = "LINK_CLICKS"; int enumVal = metaEnum.keyToValue(key); val = QVariant::fromValue(enumVal);
First Question: Why I cannot register type? I have to add * after Class Name to pass this Error
Q_DECLARE_METATYPE(BillingEvents); //error: 'QObject::QObject(const QObject&)' is private //Q_DISABLE_COPY(QObject)
Second Question: How can I get type id (int) from type name? The following code doesn't work.
const char *chr = "BillingEvents"; int type = QMetaType::type(chr);
@Dong
Hello,What I wan't is convert QString something like "APP_INSTALLS", "LINK_CLICK" to Enum Value.
If you're using
QObject
, as you do, you can use QMetaObject::enumerator and the QMetaEnum class.Q_ENUMS(EnumBillingEvents)
this seems wrong, try the Q_ENUM macroFirst Question: Why I cannot register type?
Because to declare something as meta-type you need to have a default constructor and a public copy constructor,
QObject
doesn't allow for copying, so you can't declare it as a metatype. The pointer toQObject
however, being an integral type, has all the said characteristics.Second Question: How can I get type id (int) from type name? The following code doesn't work.
QMetaType::type
requires a runtime registration of the class/type, so have you registered it with qRegisterMetaType?Kind regards.
-
-
Thanks for your quick answer.
qRegisterMetaType really does the job.
The only change i made is calling to
qRegisterMetaType<BillingEvents*>("BillingEvents");
before
const char *chr = "BillingEvents"; type = QMetaType::type(chr);
Everything work fined
-
Hi ,
not sure if this fits your situation.
convert QString something like "APP_INSTALLS", "LINK_CLICK" to Enum Value.
Maybe a hash table would work better than an enum for you, depending on how you are using you EnumBillingEvents that is.@Lineaxe :
I'm writing a Service Client for Facebook Ads API.
The Response return as JSON data and I have to convert it to Object.
I also have to communicate with other API(s) like Google, Bing, LinkedIn, ...
A Hash table could make confusion between them.
That why I need to convert QString to specific Enum Value. -
Shouldn't this:
qRegisterMetaType<BillingEvents*>("BillingEvents");
be:
qRegisterMetaType<BillingEvents>();
I would think it should.
@kshegunov
I don't know why... butqRegisterMetaType<BillingEvents>();
Cause Compile Error
error: invalid application of 'sizeof' to incomplete type 'QStaticAssertFailure<false>'
enum {Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __COUNTER __) = sizeof(QStaticAssertFailure<!!(Condition)>)} -
@kshegunov
I don't know why... butqRegisterMetaType<BillingEvents>();
Cause Compile Error
error: invalid application of 'sizeof' to incomplete type 'QStaticAssertFailure<false>'
enum {Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __COUNTER __) = sizeof(QStaticAssertFailure<!!(Condition)>)} -
@Dong
My bad.BillingEvents
is actually aQObject
and I was thinking about theEnumBillingEvents
, so callingqRegisterMetaType<BillingEvents*>();
is perfectly fine (without the string argument).Kind regards.
@kshegunov
My Enum is defined in a namespace
Without the argument I can't get type by NameI had to get it with Namespace & * as well.
type = QMetaType::type(qPrintable(QString("FacebookEnums::%1*").arg(propInfo.EnumType)));
It seem a little more complicate... but sometime it had to be.
Thanks a lots !!!