Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Q_ENUMS, Q_DECLARE_METATYPE and QML
Forum Updated to NodeBB v4.3 + New Features

Q_ENUMS, Q_DECLARE_METATYPE and QML

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 11.4k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    SteveKing
    wrote on last edited by
    #1

    I have a class which (much simplified) is:

    @class MyClass : public QObject
    {
    Q_OBJECT

    Q_ENUMS(MyEnum)
    Q_PROPERTY(MyEnum dataValue READ dataValue NOTIFY dataValueChanged)

    public:
    enum MyEnum {
    eOne,
    eTwo
    }

    ...

    };

    Q_DECLARE_METATYPE(MyClass::MyEnum)
    @

    The enum is declared in the object with Q_ENUMS so that it's visible within my QML, and the declare metatype is used so it can be accessed from a QVariant using the .value<T> call.

    The problem is that when I use the declare metatype the enum is no longer visible from the QML... My guess is that the enum is already in the meta object system by it being in the class, but this means I can't use it in the QVariant::value call.

    It feels like an obvious "You just can't do that!" but I can't find anything saying it isn't possible, any ideas?

    Thanks,
    Steve

    1 Reply Last reply
    0
    • T Offline
      T Offline
      theoriginalgri
      wrote on last edited by
      #2

      QML compares enum types with integers! If you register your Enum type, it's not an integer anymore.

      To work around this, you could write the following:

      @
      class MyClass : public QObject
      {
      Q_OBJECT

      Q_ENUMS(MyEnum)
      Q_PROPERTY(int dataValue READ declarativeDataValue NOTIFY dataValueChanged) <--- Changed

      public:
      enum MyEnum {
      eOne,
      eTwo
      }

      MyEnum dataValue() const;
      int declarativeDataValue() const; <--- Added

      ...

      };

      Q_DECLARE_METATYPE(MyClass::MyEnum)
      @

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SteveKing
        wrote on last edited by
        #3

        This does fix the problem (and it's the workaround we're using).

        I don't understand why the original code works perfectly without the Q_DECLARE_METATYPE, but when I use that it doesn't... QML comparing it to int shouldn't be affected by the declare metatype.

        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