AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
[TOC] ## 反射实例 ``` // 注册属性 Q_PROPERTY(CustomType type READ getType WRITE setType NOTIFY valueChanged) // 添加枚举 enum CustomType{ Corporate,Individual,Educational,Goverment }; Q_ENUMS(CustomType); // 反射 void setType(QString newType){ // 使用 static 只需要初始化一次 static const QMetaObject* meta = metaObject(); static int propindex = meta->indexOfProperty("type"); static const QMetaProperty mp = meta->property(propindex); QMetaEnum menum = mp.enumerator(); const char* ntype = newType.toAscii().data(); CustomType theType =static_cast<CustomType>(menum.keyToValue(ntype));; if(theType!=m_type){ CustomType oldType =m_type; m_type=theType; emit valueChanged("type",theType,oldType) } } ``` 使用反射获取传参的字符串对应的枚举值