[Qt-interest] QVariant conversion problem
Sean Harmer
sean.harmer at maps-technology.com
Thu Mar 18 20:56:24 CET 2010
Hi,
Stephen Collyer wrote:
> I have some code where the relevant defns are as follows:
>
> const QList<quint32> storytranslation_ids_by_lang
>
> QMap<QString, QVariant> where_data;
> where_data["fk_storytranslation_id"] =
> QVariant(storytranslation_ids_by_lang);
>
> g++ is complaining about the assignment in the final line, with the
> following message:
>
> error: no matching function for call to ?QVariant::QVariant(const
> QList<unsigned int>&)?
> /usr/local/Trolltech/Qt-4.6.1/include/QtCore/qvariant.h:432: note:
> candidates are: QVariant::QVariant(bool, int)
> /usr/local/Trolltech/Qt-4.6.1/include/QtCore/qvariant.h:425:
> note: QVariant::QVariant(void*)
> /usr/local/Trolltech/Qt-4.6.1/include/QtCore/qvariant.h:222:
> note: QVariant::QVariant(Qt::GlobalColor)
>
> <etc>
>
> I can't see why, as there is a QVariant ctor as follows:
>
> *QVariant* ( const QList<QVariant> & /val/ )
>
> Can someone suggest what I'm missing ?
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.
Cheers,
Sean
More information about the Qt-interest-old
mailing list