如何将 QMap 转换为 QMap >到 QVariant?

发布于 2024-09-11 03:06:33 字数 805 浏览 0 评论 0 原文

QVariantQSettings 类所需)支持从 QMap 创建,

但尝试初始化如下内容:

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

给出错误:

函数返回一个函数。

然后我尝试了 QMap 重载 QVariant() 并得到了

错误:没有匹配的函数可调用 QVariant::QVariant(QMap >&)

现在我尝试了类型转换:

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

并得到了

模板参数 2 无效
;”标记之前的声明中的类型无效

那么将嵌套的 QMap 转换为 QVariant 对象所需的巫术是什么?

QVariant (needed for QSettings class) supports creation from 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

error: no matching function for call to 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
invalid type in declaration before ';' token

So what's the required voodoo to convert a nested QMap to a QVariant object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少跟Wǒ拽 2024-09-18 03:06:33
  1. QMap)> 中,您定义了从字符串到函数类型的映射。您真正想要的是 QMap

  2. 你不想要一个QMap; > 因为这在语法上是不正确的。两个模板参数都需要是类型名称,并且类型转换不能是类型名称的一部分。

  3. QMap(或几乎任何其他类型的 QMap)放入 QVariant 中是行不通的。唯一可以转换为 QVariantQMap 类型是 QMap

    此类型的 typedef 可能有用:QVariantMap。如果您在这种情况下坚持使用QVariantMap,那么一切都会为您正常工作。

  1. In QMap<QString, QVariant(QMap<QString, QVariant>)>, you have defined a map from a string to a function type. What you really want is a QMap<QString, QVariant>.

  2. You don't want a QMap<QString,(QVariant)QMap<QString, QVariant> > because that's just syntactically incorrect. 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. The only QMap type that can be converted into a QVariant is a QMap<QString,QVariant>.

    There's a typedef for this type that may be useful: QVariantMap. If you stick to using QVariantMap for this situation, then things will work properly for you.

陈年往事 2024-09-18 03:06:33

报告的错误是 QVariant(...) 不是类型,而是函数 (c-tor)。

您应该刚刚使用: Map; i; 并仅在为映射赋值时使用 QVariant(QMap)。关键是 QVariant 确实是任何东西。因此,QVariants 的映射可以在一个位置有一个 int(包含在 QVariant 中)和一个 QDate在另一个。因此,在声明类型时,您无法指定希望 QVariant 保存哪些类型。

The error being reported is that QVariant(...) is not a type, but a function (c-tor).

You should have just used: Map<QString, QVariant> i; and used QVariant(QMap<QString, QVariant>) only when assigning values to the map. The point is QVariant is anything really. So a map of QVariants, can have an int in one position (contained in the QVariant) and a QDate in another. So when declaring the type, you can't specify which types you want QVariant to hold.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文