[solved] std::vector to QList?



  • hello
    I would like to convert a std vector into a QList. Thing is, the type might vary, so I'd like to use a QList<QVariant> (the std vector might be int, double or string but I know this when I add the data).
    I tried the following

    @QList<QVariant> tmp = QList<int>::fromVector( QVector<int>::fromStdVector(stdVectorInt)); @

    But is fails as the int type doesn't match the qvariant type. Is ther enay way of doing this without having to loop trough the vector and add one item at a time? Thanks
    Richard



  • I don't think there's a way for this automatic conversion, because there is AFAIK no way to set

    @QList<X> = QList<Y>@

    because these are different datatypes and the compiler does not know how to convert them.

    You could probably subclass QList to do that conversion, but I think this would require looping through the list - but on the other hand I think that's what is done anyway.



  • You can use std::copy in combination with a std::back_inserter to achieve that in a one-liner. This is effectively the same any "fromStdList" method would do.



  • Thank you for your replies. A related question, can I store a QVariantList inside another QVariantList? Example

    a contains 3 QVariants
    b contains 4 QVariants

    so that:

    c contains a & b -> c.size() == 2
    Thanks!
    Richard



  • Found the solution. The problem was I was using the << operator which apparently just added the individual QVariants of the list, not the list itself. Got it working by using .append instead.

    • Richard

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.