How do I cast Qt's QVariant to boost::any?
+3
A:
i dont think there is an easy way, i would do following:
boost::any qvariant_to_any(const QVariant& v) {
switch(v.userType()) {
case QVariant::Bool:
return boost::any(v.value<bool>());
//or: return boost::any(v.toBool());
case QVariant::Int:
return boost::any(v.value<int>());
//or: return boost::any(v.toInt());
case QVariant::UInt:
return boost::any(v.value<unsigned>());
// ...
// all your types which store in a QVariant in your use case
case QVariant::Invalid:
default:
throw std::bad_cast(); //or return default constructed boost::any
}
}
if Boost.Variant would do the job aswell instead of Boost.Any, in a german magazin there was a nice article about converting QVariant to Boost.Variant and vice versa, take a look at the source code if this interrests you:
german article: http://www.heise.de/developer/artikel/Konvertierungen-992950.html
source: ftp://ftp.heise.de/pub/ix/developer/elfenbein.zip
smerlin
2010-06-16 09:58:40
Fantastic answer! Thank you.
Neil G
2010-06-16 16:13:48