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. QMetaObject and QMetaProperty - why 2 times the same object?
Forum Updated to NodeBB v4.3 + New Features

QMetaObject and QMetaProperty - why 2 times the same object?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 157 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.
  • Q Offline
    Q Offline
    qwe3
    wrote on last edited by
    #1

    Hi,

    I would like to read a property from one object. I can do that, but I have to write:

    const QMetaObject * metaObj = myObject->metaObject();
    
    and in a loop:
    
    QVariant property = metaObj->property(i).read(myObject);
    

    Why I have to add myObject param in read() too ?

    I use myObject here:
    const QMetaObject * metaObj = myObject->metaObject();
    and here:
    QVariant property = metaObj->property(i).read(myObject);

    This looks very bad, that I have to use the same word in two places.

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      QMetaObject is a meta object for a class, not an instance. myObject->metaObject() is basically the same as MyObjectClass::staticMetaObject. It's just a shortcut to look up the class of a given instance.

      Properties on the other hand have values per object. When you have a meta object , which, again, is a class descriptor and not related to any particular instance of it, you can use it to read a property. The "location" of the property in given class is described in the meta object, but to get a value from a particular instance of that class you need to pass that instance.

      So, if you want to avoid using the instance pointer twice you can do it like this:

      QVariant property = MyObjectClass::staticMetaObject.property(i).read(myObject);
      

      If you don't know the class of the object you have to use myObject->metaObject() so that it gives you the meta object of the right class.

      1 Reply Last reply
      4

      • Login

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