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

Jerome Vizcaino vizcaino_jerome at yahoo.fr
Sun Jul 17 22:29:50 CEST 2011


It seems like a nice solution : we could have a default implentation for 
qVariantCanConvert that returns false unless qMetaTypeId<T>() == m_type

Also, adding an optionnal bool* to the convert() function might be a good idea 
(to check on errors).

On Sunday 17 July 2011, Konstantin Ritt wrote:
> why not something simple like:
> 
> template <typename T> inline bool QVariant::canConvert() const
> { return m_type >= QVariant::User ? qVariantCanConvert<T>(*this) : /*
> internal implementation */; }
>  template <typename T> inline T QVariant::convert() const
> { return m_type >= QVariant::User ? qVariantValue<T>(*this) : /* internal
> implementation */; }
> ?
> it makes it possible to convert to an arbitrary type rather than just to
> QString...
> 
> regards,
> Konstantin
> 
> 
> 
> 2011/7/17 Jerome Vizcaino <vizcaino_jerome at yahoo.fr>
> 
> > There's a good point on having a generic cast operation but I think this
> > is more
> > complicated to implement.
> > 
> > QVariant uses templates functions and template specializing to build
> > construct
> > and delete helper. I think the same can be done to add toString()
> > fromString()
> > features without actually requiring any modification on encapsulated
> > classes (see
> > QDataStream or QTextStream operators)
> > 
> > This can lead to :
> > 
> > QString QVariant::toString()
> > {
> > 
> >        if(m_type >= QVariant::User)
> >        
> >                // cast local pointer to correct encapsulated class
> >                // call some specialised template function to do the work
> >        
> >        else
> >        
> >                // keep the good old internal implementation for QVariant
> > 
> > native types
> > }
> > 
> > On Friday 15 July 2011, Konrad Rosenbaum wrote:
> > > Hi,
> > > 
> > > On Thu, July 14, 2011 13:18, Jerome Vizcaino wrote:
> > > > 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.
> > > 
> > > I have found myself wanting this feature as well. However, I can't
> > > think of a way to implement this without adding more requirements on
> > > classes that are compatible with QVariant.
> > > 
> > > I.e. it should be done so that classes that cannot be transformed to
> > > QString (yet) are still storable in QVariant. This seems to require a
> > > lot of dark magic... ...any ideas?
> > > 
> > >     Konrad
> > > 
> > > _______________________________________________
> > > Qt5-feedback mailing list
> > > Qt5-feedback at qt.nokia.com
> > > http://lists.qt.nokia.com/mailman/listinfo/qt5-feedback
> > 
> > _______________________________________________
> > Qt5-feedback mailing list
> > Qt5-feedback at qt.nokia.com
> > http://lists.qt.nokia.com/mailman/listinfo/qt5-feedback



More information about the Qt5-feedback mailing list