How do I cast Qt's QVariant to boost::any?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With