Results 1 to 10 of 10

Thread: Class name from QmetaProperty

  1. #1
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Class name from QmetaProperty

    hi ,
    i am working on making my own designer widget that looks and functions like qt.But now i needed to know how the property is created.I knew we can get the properties of an widget using QmetaObject and QMetaProperty but my question is will i be able to get the class name of each property.Like object name property comes from QObject and geomentry property comes from QWidget.Is it so that i should hard code myself to model or is there a way to get the classInfo from property.I have attached the image which im trying to achieve any response or related post is appreciated.

    thanks,
    Attached Images Attached Images

  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: Class name from QmetaProperty

    I am sure the property editor does this by going through the inheritance hierarchy.

    I.e. it goes down to the QMetaObject fof QObject, lists its properties, then to the QMetaObject of the first derived class, and so on.

    QMetaObject:ropertyOffset() tells you where the properties of the specific level start.

    Cheers,
    _

  3. #3
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Class name from QmetaProperty

    thanks for the reply @anda_skoa but according to doc's the offset is sum of all the properties so i cannot differentiate between QpushButton and QlineEdit so now what can be done to do so.Is there any similiar function to do like we pass object we get property as well as their superclass.

  4. #4
    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: Class name from QmetaProperty

    There is no widget that is a subclass of both QLineEdit and QPushButton.

    Cheers,
    _

  5. #5
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Class name from QmetaProperty

    i don't want to subclass all i wanted is identify class name for each property that comes from qmetaObject.

  6. #6
    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: Class name from QmetaProperty

    Quote Originally Posted by anbu01 View Post
    i don't want to subclass all i wanted is identify class name for each property that comes from qmetaObject.
    I already answered that. You came up with some kind of situation that is not possible to exist.

    Cheers,
    _

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Class name from QmetaProperty

    Quote Originally Posted by anbu01 View Post
    i don't want to subclass all i wanted is identify class name for each property that comes from qmetaObject.
    Seems to be fairly straightforward.

    You start with an instance of a QObject sub-class, e.g. MyTextEdit. Find its QMetaObject pointer. Repeat until the metaobject pointer is null:
    • extract the className(),
    • enumerate the properties coming from that class (see QMetaObject::propertyCount()), and
    • use QMetaObject::superClass() to get the parent class meta object.

    By now you could have built a map from property name to class name, which is most useful if you are building a Designer-like editor.

    For a one-off lookup you can just scan backward until metaObject->propertyOffset() is less than or equal to the property index for the desired property.

    A full example:
    Qt Code:
    1. #include <QApplication>
    2. #include <QLabel>
    3. #include <QMetaObject>
    4. #include <QMetaProperty>
    5. #include <QDebug>
    6.  
    7. class Label: public QLabel
    8. {
    9. Q_OBJECT
    10. Q_PROPERTY(int floober READ floober WRITE setFloober)
    11.  
    12. public:
    13. Label(QWidget *p = 0): QLabel(p), m_floober(0) { }
    14.  
    15. int floober() const { return m_floober; }
    16. void setFloober(int value) { m_floober = value; }
    17. private:
    18. int m_floober;
    19. };
    20.  
    21.  
    22. QString classForProperty(QObject *object, const char *name)
    23. {
    24. if (object) {
    25. const QMetaObject *mo = object->metaObject();
    26. const int index = mo->indexOfProperty(name);
    27. if (index != -1) {
    28. while (mo && mo->propertyOffset() > index)
    29. mo = mo->superClass();
    30. return QString::fromLatin1(mo->className());
    31. }
    32. }
    33. return QString();
    34. }
    35.  
    36. int main(int argc, char **argv)
    37. {
    38. QApplication app(argc, argv);
    39. Label l;
    40.  
    41. // To develop a complete map
    42. const QMetaObject *mo = l.metaObject();
    43. while (mo) {
    44. qDebug() << "From" << mo->className();
    45. for (int i = mo->propertyOffset(); i < mo->propertyCount(); ++i) {
    46. qDebug() << " " << mo->property(i).name();
    47. }
    48. mo = mo->superClass();
    49. }
    50.  
    51. // Use an as-required lookup
    52. qDebug() << classForProperty(&l, "floober");
    53. qDebug() << classForProperty(&l, "modal");
    54. qDebug() << classForProperty(&l, "doesNotExist");
    55.  
    56. return 0;
    57. }
    58. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 30th April 2014 at 01:15.

  8. The following user says thank you to ChrisW67 for this useful post:

    anbu01 (30th April 2014)

  9. #8
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Class name from QmetaProperty

    @guru thanks this is wat im looking for......

  10. #9
    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: Class name from QmetaProperty

    Ah, too lazy to do it yourself?
    You could have said so instead of creating mythical obstacles like classes inheriting from two widget base classes

    Cheers,
    _

  11. #10
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Class name from QmetaProperty

    @guru im not qt certified developer , i had some logics but it didnt work.....

Similar Threads

  1. Replies: 5
    Last Post: 13th January 2014, 10:24
  2. Replies: 2
    Last Post: 3rd June 2013, 17:45
  3. Replies: 4
    Last Post: 4th July 2012, 08:37
  4. Replies: 3
    Last Post: 27th December 2008, 20:34
  5. Replies: 3
    Last Post: 18th May 2008, 19:36

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.