How do I convert QMap<QString, QMap<QString, int> > to a QVariant?
Asked Answered
A

2

6

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?

Anissaanita answered 19/7, 2010 at 13:41 Comment(0)
S
4

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.

Subcartilaginous answered 19/7, 2010 at 13:51 Comment(1)
Thanks, i think i had my thought processes going the wrong way, (When all you have is a hammer...), i've settled on using QList<QVariantMap>Anissaanita
O
6
  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.

Oyster answered 19/7, 2010 at 14:3 Comment(1)
Looking over some intricate C++ details, my point #1 isn't totally correct: I thought that QVariant(QMap<QString, QVariant>) it's a function type because std::function (from C++0x) accepts it as a function type, but it's actually something more complicated -- std::function makes it look like a function type, while it actually uses partial specialization and varadic templates to make it work.Oyster
S
4

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.

Subcartilaginous answered 19/7, 2010 at 13:51 Comment(1)
Thanks, i think i had my thought processes going the wrong way, (When all you have is a hammer...), i've settled on using QList<QVariantMap>Anissaanita

© 2022 - 2024 — McMap. All rights reserved.