Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 105
Registered: ‎09-06-2013
My Device: Z10
Accepted Solution

[QVariant] Automatic conversion to its respective type?

Sometimes my QVariantList contain QVariant of QString, QDateTime etc, is there a QVariant method to convert to thier respective type automatically? 

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: [QVariant] Automatic conversion to its respective type?

[ Edited ]

Hi,

 

You can define a helper function in .h file:

 

template <typename T>
inline void getValue(const QVariant &v, T &dest)
{
  dest = v.value<T>();
}

Sample usage:

QVariant v;
QString s;
getValue(v, s);

 

 Alas, C++ cannot automatically deduce the type from return value, so this won't work:

template <typename T>
inline T getValue(const QVariant &v)
{ ... }

// requires the type to be specified explicitly: s = getValue<QString>(v)

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 105
Registered: ‎09-06-2013
My Device: Z10

Re: [QVariant] Automatic conversion to its respective type?

[ Edited ]

Say I have a QVariantList with a QString, a boolean and a QDateTime all wrapped by a QVariant each

 

I know I can determine its a QVariant type using :

 

QVariant::type() which returns its type.

 

Why isnt there a method that accepts Type and returns the value inside the QVariant cast  into the specified type already?

 

EDIT: Above comment marked as answer