Results 1 to 3 of 3

Thread: QMetaEnum, QMetaProperty for dynamically added property

  1. #1
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QMetaEnum, QMetaProperty for dynamically added property

    I need to come up with a property browser (for dynamic properties) like interface table for QObject derived class objects. Properties can be enumerated using QObject::dynamicPropertyNames() function. For each entry, I intend to read the property from the object using QObject:roperty(). That will take care of the reading part.
    For modifying data, I need to depend on the type of variant returned by QObject:roperty() and set the delegate appropriately for the type. Here comes the problem with enumeration types. For enum, I want to have a delegate which shows a combo box with all the enumeration value strings. For Q_Property like properties(lets call it static properties), I can access the metaObject(), determine whether enum, get the QMetaEnum using QMetaProp::enumerator() and get it done easily.
    However, dynamic properties the QVariant 1st problem is type is int for the enums. I can overcome that by Q_Declare_MetaType(SomeClass::EnumName) and qRegisterMetaType<SomeClass::EnumName>("nameMyEnum ").
    Now the QVariant I get from QObject:roperty() will give the type as "nameMyEnum" rather than int.
    But how do i get the QMetaEnum so that I can get all the related enum values and populate the combo box.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMetaEnum, QMetaProperty for dynamically added property

    Make SomeClass a QObject, create some mapping of enum name to meta object, e.g.
    Qt Code:
    1. QHash<QByteArray, QMetaObject*> EnumMap;
    2. EnumMap.insert("nameMyEnum", &SomeClass::staticMetaObject);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMetaEnum, QMetaProperty for dynamically added property

    Thanks for the suggestion.
    SomeClass is already a QObject derived class. Had to make it one to use Q_ENUMS, Q_DECLARE_METATYPE, qRegisterMetaType. However, there are a few of these SomeClass like classes. Currently runs into hundreds, and expected to grow further. Hardcoding for each one of these class into a hash will be too prone to break. I will have to introduce some kind of registration scheme for each class.
    Even that will end up being messy, given the fact that the enums types are int in qvariant. To get them to show some type I need to qRegisterMetaType for each of these. Given most of the enums of qt gui types are not registered, the code will be too prone to error as the author of a class may not introduce the qRegisterMetaType for each type.
    I wish there was a easier way out, like it is with static properties iteration and type classification. Too much to ask for, perhaps.

Similar Threads

  1. Replies: 8
    Last Post: 2nd February 2012, 19:52
  2. Replies: 5
    Last Post: 14th December 2011, 11:51
  3. Replies: 1
    Last Post: 28th October 2010, 22:36
  4. Connecting dynamically added properties
    By stefkeB in forum Qt Programming
    Replies: 1
    Last Post: 5th December 2008, 15:19
  5. QMetaEnum
    By No-Nonsense in forum Qt Programming
    Replies: 6
    Last Post: 7th February 2007, 15:15

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.