Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I cast Qt's QVariant to boost::any?

Tags:

c++

boost

qt

How do I cast Qt's QVariant to boost::any?

like image 788
Neil G Avatar asked Dec 12 '22 21:12

Neil G


1 Answers

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

like image 152
smerlin Avatar answered Dec 25 '22 04:12

smerlin