Example:
1. 声明自定义类型可用于QVariant,类也能用,也是这样,QT的基本数据类型不用声明就可以用,而且存入是什么类型,拿出来还是什么类型
#include // Important,这个头文件
#include
struct Student{
int id;
std::string name;
std::string email;
};
Q_DECLARE_METATYPE(Student)//和 这句注册?声明的话
2. 转换
QVariant variant = QVariant::fromValue(Student());//从 student 的对象转化过来的 QVariant 对象
if(variant.canConvert()){//判断防止空指针
Student stu = variant.value();//用value方法带上转换的类型就是了
}
reference
- https://blog.csdn.net/dayi7290/article/details/102051974
- https://blog.csdn.net/qq78442761/article/details/82084295