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. [Solved] QVariant and enums

[Solved] QVariant and enums

Scheduled Pinned Locked Moved General and Desktop
10 Posts 6 Posters 33.1k 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
    DSav
    wrote on 12 Jun 2011, 10:36 last edited by
    #1

    It seems that QVariant::value() won’t work correctly with enums. It’s of course possible to use this, but it looks like hacking:
    @
    QVariant variant = enumValue; // EnumType enumValue
    // ...
    EnumType anotherValue = static_cast<EnumType>(variant.toInt());
    @
    Is there a cute way to obtain enum value from QVariant using QVariant::value()?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      loladiro
      wrote on 12 Jun 2011, 15:29 last edited by
      #2

      Did you declare the enum with "Q_DECLARE_METATYPE()":http://doc.qt.nokia.com/4.7/qmetatype.html#Q_DECLARE_METATYPE ? If that still doesn't work you could wrap the enum in a struct (I know that works)

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DSav
        wrote on 12 Jun 2011, 15:32 last edited by
        #3

        Yes, I’ve definitely declared. Wrapping in a struct looks like a kind of hacking as well :)

        1 Reply Last reply
        0
        • L Offline
          L Offline
          loladiro
          wrote on 12 Jun 2011, 15:48 last edited by
          #4

          Consider the following example:
          @

          class Test2
          {
          public:
          Test2() {}
          enum Test { V1=100, V2, V3 };
          };

          Q_DECLARE_METATYPE(Test2::Test)

          int main(int argc, char *argv[])
          {
          QVariant v1 = Test2::V1;
          qDebug() << v1.toInt(0) << v1.valueTest2::Test();

          QVariant v2 = qVariantFromValue(Test2::V1);
          qDebug() << v2.toInt(0) << v2.value<Test2::Test>();
          return 0;
          

          }
          @

          The output is:
          @100 0
          0 100 @
          Does that help?

          1 Reply Last reply
          1
          • D Offline
            D Offline
            DSav
            wrote on 12 Jun 2011, 17:17 last edited by
            #5

            Yes, that is helpful, thanks.

            1 Reply Last reply
            0
            • V Offline
              V Offline
              ViRuSTriNiTy
              wrote on 12 Jul 2012, 13:56 last edited by
              #6

              Hi,

              one side note: when using Q_DECLARE_METATYPE() on an enum you define it as user type in the Qt meta system. The drawback is that e.g. QMetaProperty::isEnumType() won't work anymore because its now a user defined type. Qt designer won't accept it either.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lgeyer
                wrote on 12 Jul 2012, 18:18 last edited by
                #7

                Please elaborate.
                @
                class Test : public QObject
                {
                Q_OBJECT
                Q_ENUMS(Value)
                Q_PROPERTY(Value value READ value WRITE setValue)

                public:
                enum Value { valueA, valueB, valueC };

                void setValue(Value value) { value_ = value; }
                Value value() const { return value_; }
                

                private:
                Value value_;
                };

                Q_DECLARE_METATYPE(Test::Value)

                qDebug() << Test().metaObject()->property(1).isEnumType(); // true
                @

                1 Reply Last reply
                0
                • V Offline
                  V Offline
                  ViRuSTriNiTy
                  wrote on 6 Sept 2012, 07:38 last edited by
                  #8

                  @Lukas: What do you mean by "Please elaborate"?

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on 6 Sept 2012, 10:06 last edited by
                    #9

                    It seems that Lukas is demonstrating with a small code snippet that the claim made by ViRuSTrNiTy is untrue.

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      bam80
                      wrote on 18 Apr 2014, 12:59 last edited by
                      #10

                      [quote author="loladiro" date="1307893708"]Consider the following example:
                      @

                      class Test2
                      {
                      public:
                      Test2() {}
                      enum Test { V1=100, V2, V3 };
                      };

                      Q_DECLARE_METATYPE(Test2::Test)

                      int main(int argc, char *argv[])
                      {
                      QVariant v1 = Test2::V1;
                      qDebug() << v1.toInt(0) << v1.valueTest2::Test();

                      QVariant v2 = qVariantFromValue(Test2::V1);
                      qDebug() << v2.toInt(0) << v2.value<Test2::Test>();
                      return 0;
                      

                      }
                      @
                      [/quote]

                      Thank you. The enum doesn't need to be wrapped in a class.
                      Following works:
                      @

                      #include <QtDebug>

                      enum Test { V1=100, V2, V3 };

                      Q_DECLARE_METATYPE(Test)

                      int main(int argc, char *argv[])
                      {
                      QVariant v1 = V1;
                      qDebug() << v1.toInt(0) << v1.value<Test>();

                      QVariant v2 = QVariant::fromValue(V1);
                      qDebug() << v2.toInt(0) << v2.value<Test>();
                      

                      }
                      @

                      1 Reply Last reply
                      1

                      • Login

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