ユーザー定義型にはQVariant::userType()があります。QVariant::type() のように機能しますが、ユーザー定義型の型 ID 整数を返しますが、QVariant::type() は常に QVariant::UserType を返します。
タイプの名前を文字列として返すQVariant::typeName()もあります。
編集 :
おそらく、QVariant の設定方法によって異なります。QVariant::QVariant(int type, const void * copy)を直接使用することは推奨されません。
次のような 3 つのタイプがあるとします。
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_DECLARE_METATYPE のない 3 番目
私はそれらを 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