anbu selvan
anbu selvan

Reputation: 745

Class name from QMetaProperty

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, enter image description here

Upvotes: 1

Views: 790

Answers (1)

vahancho
vahancho

Reputation: 21230

In order to create mapping between classes and their properties you need to traverse through your object's class hierarchy. Here is the sample code that crates such mapping:

// Create properties per class mapping for the initialObject.
const QMetaObject *mo = initialObject->metaObject();
QMap<QString, QStringList> propertyMap;
do {
    QStringList properties;
    for(int i = mo->propertyOffset(); i < mo->propertyCount(); ++i) {
        properties << QString::fromLatin1(mo->property(i).name());
    }
    propertyMap[mo->className()] = properties;

} while (mo = mo->superClass());

Now the propertyMap contains all properties sorted by the class names they belong to.

Upvotes: 2

Related Questions