问题
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
回答1:
QList<T>
isn't really a pre-defined variant type (except QList<QVariant>
) so it technically resolves to a QMetaType::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 example QVariant::typeToName(v.userType())
prints QList<int>
correctly. I've found that QVariant::userType()
always returns the actual meta type, whereas just type()
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 for type() it goes to the "obsolete members" section. Where that enum isn't even listed.
Actually QMetaType
doesn't "know" the QList<int>
type either until it is used (or registered). Example:
qDebug() << QMetaType::type("QList<int>"); // prints 0
QList<int> list{1,2,3};
QVariant v = QVariant::fromValue(list);
qDebug() << QMetaType::type("QList<int>"); // prints 1028 on my test project, yours may vary
Or, if registered specifically:
qDebug() << qRegisterMetaType<QList<int> >("QList<int>"); // prints 1028
qDebug() << QMetaType::type("QList<int>"); // prints 1028
QList<int> list{1,2,3};
QVariant v = QVariant::fromValue(list);
qDebug() << QMetaType::type("QList<int>"); // still 1028
来源:https://stackoverflow.com/questions/58158034/qlist-in-qvariant-qvarianttype-returns-weird-type