I'm trying to store QList<int>
in QVariant
and then do a typecheck to determine exact type of value stored in QVariant
(for serialization).
QVariant::type()
works fine with scalar types like int
or QString
, but this is what I got with QList<int>
:
QList<int> list{1,2,3};
QVariant v = QVariant::fromValue<QList<int>>(list);
qDebug() << v.type() << "|" << v.typeName() << "|" << v.userType() << "|" << QVariant::typeToName(v.type());
Produces:
QVariant::QWidget* | QList<int> | 1030 | QWidget*
Where this QVariant::QWidget*
even came from? I can't find it in QVariant's enum Type
, nor value 1030. Q_DECLARE_METATYPE(QList<int>);
has no effect.
What's wrong here? Or should I use typeName()
since it returns correct type name instead of type()
?
Qt 5.13.1, clang 8.0.1, Linux 64-bit
QList<T>
isn't really a pre-defined variant type (exceptQList<QVariant>
) so it technically resolves to aQMetaType::UserType
. It's a bit convoluted to follow, and definitely depends on which Qt modules have been included (the meta type system is designed to be expandable this way -- eg. w/out GUI module you wouldn't have any of those types available).So... it's actually
QMetaType
that "knows" about the underlying custom types. From given exampleQVariant::typeToName(v.userType())
printsQList<int>
correctly. I've found thatQVariant::userType()
always returns the actual meta type, whereas justtype()
seems fairly useless (even the docs are convoluted trying to explain how it works).To add to the confusion,
QVariant::Type
seems to be deprecated... if you click the "QVariant::Type" link in the docs fortype()
it goes to the "obsolete members" section. Where that enum isn't even listed.Actually
QMetaType
doesn't "know" theQList<int>
type either until it is used (or registered). Example:Or, if registered specifically: