简体   繁体   English

我如何转换QMap <QString, QMap<QString, int> &gt;到QVariant?

[英]How do I convert QMap<QString, QMap<QString, int> > to a QVariant?

QVariant (needed for QSettings class) supports creation from QMap<QString, QVariant> QVariantQSettings类需要)支持从QMap<QString, QVariant>

But trying to initialise something like this: 但试图初始化这样的事情:

QMap<QString, QVariant(QMap<QString, QVariant>)> i;

Gives the error: 给出错误:

function returning a function. 函数返回一个函数。

So then I tried the QMap<QString, QVariant> overload for QVariant() and got 然后我为QVariant()尝试了QMap<QString, QVariant>重载并得到了

error: no matching function for call to QVariant::QVariant(QMap<QString, QMap<QString, int> >&) 错误:没有用于调用QVariant::QVariant(QMap<QString, QMap<QString, int> >&)匹配函数QVariant::QVariant(QMap<QString, QMap<QString, int> >&)

Now I tried a typecast: 现在我尝试了一个类型转换:

QMap<QString, (QVariant)QMap<QString, QVariant> > i;

and got 得到了

template argument 2 is invalid 模板参数2无效
invalid type in declaration before ' ; '之前声明中的无效类型' ; ' token '令牌

So what's the required voodoo to convert a nested QMap to a QVariant object? 那么将嵌套QMap转换为QVariant对象所需的巫术是什么?

  1. In QMap<QString, QVariant(QMap<QString, QVariant>)> , you have defined a map from a string to a function type. QMap<QString, QVariant(QMap<QString, QVariant>)> ,您已经定义了从字符串到函数类型的映射。 What you really want is a QMap<QString, QVariant> . 你真正想要的是QMap<QString, QVariant>

  2. You don't want a QMap<QString,(QVariant)QMap<QString, QVariant> > because that's just syntactically incorrect. 你不需要QMap<QString,(QVariant)QMap<QString, QVariant> >因为这只是语法错误。 Both template parameters need to be type names, and typecast can't be part of at type name. 两个模板参数都需要是类型名称,并且类型名称不能成为类型名称的一部分。

  3. Putting a QMap<QString, int> (or almost any other type of QMap ) into a QVariant won't work. QMap<QString, int> (或几乎任何其他类型的QMap )放入QVariant将不起作用。 The only QMap type that can be converted into a QVariant is a QMap<QString,QVariant> . 唯一可以转换为QVariant QMap类型是QMap<QString,QVariant>

    There's a typedef for this type that may be useful: QVariantMap . 这种类型的typedef可能很有用: QVariantMap If you stick to using QVariantMap for this situation, then things will work properly for you. 如果您坚持在这种情况下使用QVariantMap ,那么事情将适合您。

The error being reported is that QVariant(...) is not a type, but a function (c-tor). 报告的错误是QVariant(...)不是类型,而是函数(c-tor)。

You should have just used: Map<QString, QVariant> i; 你应该刚刚使用过: Map<QString, QVariant> i; and used QVariant(QMap<QString, QVariant>) only when assigning values to the map. 并且仅在为地图指定值时使用QVariant(QMap<QString, QVariant>) The point is QVariant is anything really. 问题的关键是QVariant什么真正的。 So a map of QVariants , can have an int in one position (contained in the QVariant ) and a QDate in another. 所以地图QVariants ,可以具有int在一个位置(包含在QVariant )和QDate在另一个。 So when declaring the type, you can't specify which types you want QVariant to hold. 因此,在声明类型时,您无法指定要将QVariant保留的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM