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. Qt property system: access enumerations as string
Forum Updated to NodeBB v4.3 + New Features

Qt property system: access enumerations as string

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.5k 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.
  • L Offline
    L Offline
    lfdm
    wrote on last edited by
    #1

    Hi fellows!

    I have probably a novice question in here. I'm using the property system to share settings between different class.

    I need an enumeration and basically followed the documentation example:
    @class MyClass : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
    Q_ENUMS(Priority)

    public:
    MyClass(QObject *parent = 0);
    ~MyClass();

     enum Priority { High, Low, VeryHigh, VeryLow };
    
     void setPriority(Priority priority)
     {
         m_priority = priority;
         emit priorityChanged(priority);
     }
     Priority priority() const
     { return m_priority; }
    

    signals:
    void priorityChanged(Priority);

    private:
    Priority m_priority;
    };@

    @MyClass *myinstance = new MyClass;
    QObject *object = myinstance;

    myinstance->setPriority(MyClass::VeryHigh);
    object->setProperty("priority", "VeryHigh");@

    Where I don't find information is how to get in this case "priority" returning for example "VeryHigh" this QVariant instead of 2
    @object->property("priority").toString() == "VeryHight";@

    Or did I get it wrong?

    Thanks for any help,
    -Damien

    Damien LEFEVRE - http://www.lfdm.net

    1 Reply Last reply
    0
    • V Offline
      V Offline
      veeraps
      wrote on last edited by
      #2

      One thing I found (of course from web) is

      @
      #include <QMetaObject>
      #include <QMetaEnum>

      QMetaObject metaObject = MyClass::staticMetaObject;
      QMetaEnum metaEnum = metaObject.enumerator( metaObject.indexOfEnumerator( "Priority" ) );
      
      MyClass* myinstance = new MyClass( 0 );
      myinstance->setPriority( MyClass::VeryHigh );
      QString enumVal = QString( metaEnum.valueToKey( myinstance->property( "priority" ).toInt() ) );
      

      @

      The other way would be constructing a method to convert enum to QString kind of mapping method to map enum value to respective QString.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lfdm
        wrote on last edited by
        #3

        Thanks!

        I'll give it a go.

        Damien LEFEVRE - http://www.lfdm.net

        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