[Qt-interest] QVariant conversion problem

Stephen Collyer scollyer at netspinner.co.uk
Fri Mar 19 16:15:25 CET 2010


On 18 March 2010 19:56, Sean Harmer <sean.harmer at maps-technology.com> wrote:

>
> You need to put your data into a QVariant first. There is no QVariant
> constructor that takes your type, so you need something like:
>
> const QList<quint32> storytranslation_ids_by_lang
> QMap<QString, QVariant>   where_data;
> QVariant v;
> v.setValue(storytranslation_ids_by_lang);
> where_data["fk_storytranslation_id"] = v;
>
> Note that you also need to tell QMetaType about your new type that you
> wish to store within QVariant too. You can do this with the
> Q_DECLARE_METATYPE() macro.
>
>
Right, this seems like the way to go though I'm not sure of
some details. I have satisfied the compiler by declaring:

typedef QList<quint32>          quint32List;
Q_DECLARE_METATYPE(quint32List)

and assigning the value via QVariant::setValue() as you suggested.

However, I seem to run into another problem later on, where I perform
a QVariant::canConvert(QVariant::List) test on the value assigned
using QVariant::setValue() - this returns false. Does this suggest that
the QVariant::canConvert() is not aware of the Q_DECLARE_METATYPE
declaration for some reason ?

I'm also not entirely happy with my passing the typedef to
Q_DECLARE_METATYPE, as the somewhat scanty documentation seems
to suggest that it needs to see a full class definition (with default and
copy ctors,
as well).

-- 
Stephen Collyer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100319/924103c6/attachment.html 


More information about the Qt-interest-old mailing list