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.
  • DongD Offline
    DongD Offline
    Dong
    wrote on 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);
    
    kshegunovK 1 Reply Last reply
    0
    • DongD Dong

      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);
      
      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on 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
      • DongD Offline
        DongD Offline
        Dong
        wrote on 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

        kshegunovK 1 Reply Last reply
        0
        • DongD Dong

          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

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on 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

          DongD 1 Reply Last reply
          0
          • L Offline
            L Offline
            Lineaxe
            wrote on 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.

            DongD 1 Reply Last reply
            0
            • L Lineaxe

              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.

              DongD Offline
              DongD Offline
              Dong
              wrote on 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
              • kshegunovK kshegunov

                @Dong

                Shouldn't this:

                qRegisterMetaType<BillingEvents*>("BillingEvents");
                

                be:

                qRegisterMetaType<BillingEvents>();
                

                I would think it should.

                DongD Offline
                DongD Offline
                Dong
                wrote on 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)>)}

                kshegunovK 1 Reply Last reply
                0
                • DongD Dong

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

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on 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

                  DongD 1 Reply Last reply
                  0
                  • kshegunovK kshegunov

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

                    DongD Offline
                    DongD Offline
                    Dong
                    wrote on last edited by Dong
                    #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

                    • Login

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