Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. ENUM value from QString

ENUM value from QString

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 11.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dong
    wrote on 2 Mar 2016, 08:39 last edited by
    #1

    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);
    
    K 1 Reply Last reply 2 Mar 2016, 08:51
    0
    • D Dong
      2 Mar 2016, 08:39

      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);
      
      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 2 Mar 2016, 08:51 last edited by
      #2

      @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 macro

      First 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 to QObject 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.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • D Offline
        D Offline
        Dong
        wrote on 2 Mar 2016, 09:49 last edited by
        #3

        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

        K 1 Reply Last reply 2 Mar 2016, 09:51
        0
        • D Dong
          2 Mar 2016, 09:49

          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

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 2 Mar 2016, 09:51 last edited by
          #4

          @Dong

          Shouldn't this:

          qRegisterMetaType<BillingEvents*>("BillingEvents");
          

          be:

          qRegisterMetaType<BillingEvents>();
          

          I would think it should.

          Read and abide by the Qt Code of Conduct

          D 1 Reply Last reply 3 Mar 2016, 08:23
          0
          • L Offline
            L Offline
            Lineaxe
            wrote on 2 Mar 2016, 12:55 last edited by
            #5

            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.

            D 1 Reply Last reply 3 Mar 2016, 04:33
            0
            • L Lineaxe
              2 Mar 2016, 12:55

              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.

              D Offline
              D Offline
              Dong
              wrote on 3 Mar 2016, 04:33 last edited by
              #6

              @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.

              1 Reply Last reply
              0
              • K kshegunov
                2 Mar 2016, 09:51

                @Dong

                Shouldn't this:

                qRegisterMetaType<BillingEvents*>("BillingEvents");
                

                be:

                qRegisterMetaType<BillingEvents>();
                

                I would think it should.

                D Offline
                D Offline
                Dong
                wrote on 3 Mar 2016, 08:23 last edited by
                #7

                @kshegunov
                I don't know why... but

                qRegisterMetaType<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)>)}

                K 1 Reply Last reply 3 Mar 2016, 11:19
                0
                • D Dong
                  3 Mar 2016, 08:23

                  @kshegunov
                  I don't know why... but

                  qRegisterMetaType<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)>)}

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 3 Mar 2016, 11:19 last edited by
                  #8

                  @Dong
                  My bad. BillingEvents is actually a QObject and I was thinking about the EnumBillingEvents, so calling qRegisterMetaType<BillingEvents*>(); is perfectly fine (without the string argument).

                  Kind regards.

                  Read and abide by the Qt Code of Conduct

                  D 1 Reply Last reply 4 Mar 2016, 02:26
                  0
                  • K kshegunov
                    3 Mar 2016, 11:19

                    @Dong
                    My bad. BillingEvents is actually a QObject and I was thinking about the EnumBillingEvents, so calling qRegisterMetaType<BillingEvents*>(); is perfectly fine (without the string argument).

                    Kind regards.

                    D Offline
                    D Offline
                    Dong
                    wrote on 4 Mar 2016, 02:26 last edited by Dong 3 Apr 2016, 02:30
                    #9

                    @kshegunov
                    My Enum is defined in a namespace
                    Without the argument I can't get type by Name

                    I 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 !!!

                    1 Reply Last reply
                    0

                    2/9

                    2 Mar 2016, 08:51

                    topic:navigator.unread, 7
                    • Login

                    • Login or register to search.
                    2 out of 9
                    • First post
                      2/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved