[Development] Converting types in Qt

Olivier Goffart olivier at woboq.com
Wed Jul 16 11:04:36 CEST 2014


On Wednesday 16 July 2014 08:41:07 Poenitz Andre wrote:
> I wholeheartedly disagree. Most of my QVariant uses are there because
> the Qt API requires me to use it, and I sometimes use it voluntarily for
> type-agnostic storage or transport of "things". But in those cases I never
> want to extract anything else from the variant than exactly the "thing"
> I put into it.

So if I understand you and Eike correctly, what you want is some kind of

template<typename T> T qvariant_cast_safe(const QVariant &v) {
	Q_ASSERT(v.userType() == qMetaTypeId<T>())
    return *reinterpret_cast<const T *>(v.constData());
}

> I _never_ (at least not intentionally) use QVariant as a kind of "magic
> converter bag" where I put something in and get something
> "conveniently" munged back.

When you play with qml or itemview, it's cool that there is a 
QVariant::toString()  that puts some string out in order to show to the user 
what's in it.

-- 
Olivier 

Woboq - Qt services and support - http://woboq.com - http://code.woboq.org



More information about the Development mailing list