对于用户定义的类型
QVariant::userType()
. 它的工作方式类似于QVariant::type(),但返回用户定义类型的type id整数,而QVariant::type()始终返回QVariant::UserType。
还有
QVariant::typeName()
编辑:
QVariant::QVariant(int type, const void * copy)
他很气馁。
class MyFirstType
{
public:
MyFirstType();
MyFirstType(const MyFirstType &other);
~MyFirstType();
MyFirstType(const QString &content);
QString content() const;
private:
QString m_content;
};
Q_DECLARE_METATYPE(MyFirstType);
第三个没有Q\u的声明\u元类型
我把它们储存在QVariant中:
QString content = "Test";
MyFirstType first(content);
MySecondType second(content);
MyThirdType third(content);
QVariant firstVariant;
firstVariant.setValue(first);
QVariant secondVariant = QVariant::fromValue(second);
int myType = qRegisterMetaType<MyThirdType>("MyThirdType");
QVariant thirdVariant(myType, &third); // Here the type isn't checked against the data passed
qDebug() << "typeName for first :" << firstVariant.typeName();
qDebug() << "UserType :" << firstVariant.userType();
qDebug() << "Type : " << firstVariant.type();
[...]
我得到:
typeName for first : MyFirstType
UserType : 256
Type : QVariant::UserType
typeName for second : MySecondType
UserType : 257
Type : QVariant::UserType
typeName for third : MyThirdType
UserType : 258
Type : QVariant::UserType