//拿到控件元对象
const QMetaObject *metaObject = widget->metaObject();
//所有属性的数量
int propertyCount = metaObject->propertyCount();
//propertyOffset是自定义的属性开始的位置
int propertyOffset = metaObject->propertyOffset();
//循环取出控件的自定义属性, int i = 0 表示所有属性
for (int i = propertyOffset; i < propertyCount; ++i) {
QMetaProperty metaProperty = metaObject->property(i);
const char *name = metaProperty.name();
const char *type = metaProperty.typeName();
QVariant value = widget->property(name);
qDebug() << name << type << value;
}
//所有方法的数量
int methodCount = metaObject->methodCount();
//methodOffset是自定义的方法开始的位置
int methodOffset = metaObject->methodOffset();
//循环取出控件的自定义方法, int i = 0 表示所有方法
for (int i = methodOffset; i < methodCount; ++i) {
QMetaMethod metaMethod = metaObject->method(i);
const char *name = metaMethod.name();
const char *type = metaMethod.typeName();
qDebug() << name << type;
}
Qt之如何获取类的属性和方法
于 2023-12-03 18:12:51 首次发布