[Qt5-feedback] QVariant::toString() and custom types

Konstantin Ritt ritt.ks at gmail.com
Thu Jul 14 16:52:30 CEST 2011


an ability to convert an arbitrary QVariant to QString only makes not too
much sense. an ability to convert QVariant to an arbitrary type sounds more
useful to me.
i.e.
class A { .. };
class B : public A { .. };
class C : public A { .. };

C c;
QVariant v = qVariantFromValue(c);

// and

Q_ASSERT(v.canConvert<A>());
A a = v.value<A>();

// instead of

Q_ASSERT(v.canConvert<A>() || v.canConvert<B>() || v.canConvert<C>());
A a;
if (v.canConvert<C>()) a = v.value<C>();
else if (v.canConvert<B>()) a = v.value<B>();
else if (v.canConvert<A>()) a = v.value<A>();

BTW, there is a task for this exists at the bug tracker:
http://bugreports.qt.nokia.com/browse/QTBUG-14940

regards,
Konstantin



2011/7/14 Jerome Vizcaino <vizcaino_jerome at yahoo.fr>

> Hi,
>
> I use QVariant a lot in Qt4 to store native but also custom types.
> One thing I find a bit annoying is that there is no way to "extend" the
> toString() QVariant functionnality.
>
> How about adding some sort of operator (à la QDataStream) that custom types
> could provide to help QVariant convert to/from string ?
>
> This would be really convienent in order to easily print QVariant without
> actually knowing if it's custom or native content.
>
> Jérôme
> _______________________________________________
> Qt5-feedback mailing list
> Qt5-feedback at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt5-feedback
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt5-feedback/attachments/20110714/d31519ac/attachment.html 


More information about the Qt5-feedback mailing list