For custom types, QVariant :: userType () . It works like QVariant :: type (), but returns an integer of type identifiers of a user-defined type, while QVariant :: type () always returns QVariant :: UserType.
There is also QVariant :: typeName () , which returns the type name as a string.
EDIT:
This probably depends on how you install QVariant. Directly using QVariant :: QVariant (int type, const void * copy) is not recommended.
Let's say I have three types:
class MyFirstType { public: MyFirstType(); MyFirstType(const MyFirstType &other); ~MyFirstType(); MyFirstType(const QString &content); QString content() const; private: QString m_content; }; Q_DECLARE_METATYPE(MyFirstType);
Third without Q_DECLARE_METATYPE
I store them in 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);
I get:
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
Leiaz
source share