[英]How do I convert QMap<QString, QMap<QString, int> > to a QVariant?
QVariant
(needed for QSettings
class) supports creation from QMap<QString, QVariant>
QVariant
( QSettings
类需要)支持从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
对象所需的巫术是什么?
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>
。
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. 两个模板参数都需要是类型名称,并且类型名称不能成为类型名称的一部分。
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.