Class name from QMetaProperty

删除回忆录丶 提交于 2019-12-11 12:23:38

问题


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,


回答1:


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.



来源:https://stackoverflow.com/questions/23359215/class-name-from-qmetaproperty

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!