QVariant中的QList,QVariant :: type()返回奇怪的类型 [英] QList in QVariant, QVariant::type() returns weird type

查看:624
本文介绍了QVariant中的QList,QVariant :: type()返回奇怪的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将QList<int>存储在QVariant中,然后进行类型检查以确定存储在QVariant中的值的确切类型(用于序列化). QVariant::type()在标量类型(例如intQString)中可以很好地工作,但这就是我在QList<int>中得到的:

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());

产生:

QVariant::QWidget* | QList<int> | 1030 | QWidget*

这个QVariant::QWidget*甚至来自哪里?我在QVariant的enum Type中找不到它,也没有值1030.Q_DECLARE_METATYPE(QList<int>);没有任何作用.

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.

这是怎么了?还是我应该使用typeName(),因为它返回正确的类型名称而不是type()?

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位

Qt 5.13.1, clang 8.0.1, Linux 64-bit

推荐答案

QList<T>实际上不是预定义的变体类型(QList<QVariant>除外),因此从技术上讲,它解析为QMetaType::UserType.这有点令人费解,并且肯定取决于所包含的Qt模块(元类型系统被设计为可以这种方式扩展-例如,没有GUI模块,您将没有任何可用的这些类型).

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).

所以...实际上是QMetaType了解"底层的自定义类型.从给定的示例QVariant::typeToName(v.userType())正确打印QList<int>.我发现QVariant::userType()总是返回实际的元类型,而type()似乎毫无用处(即使文档很费力地试图解释它的工作原理).

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).

更令人困惑的是,如果您单击QVariant::Type./cvariant.html#type"rel =" nofollow noreferrer> type() 的文档,它将转到过时的成员"部分.甚至没有列出该枚举的地方.

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.

实际上,QMetaType在使用(或注册)之前都不知道" QList<int>类型.示例:

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

或者,如果专门注册:

    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

这篇关于QVariant中的QList,QVariant :: type()返回奇怪的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆