[solved] std::vector to QList?
-
wrote on 24 Apr 2012, 10:35 last edited by
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 -
wrote on 24 Apr 2012, 10:52 last edited by
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.
-
wrote on 24 Apr 2012, 17:40 last edited by
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.
-
wrote on 25 Apr 2012, 12:14 last edited by
Thank you for your replies. A related question, can I store a QVariantList inside another QVariantList? Example
a contains 3 QVariants
b contains 4 QVariantsso that:
c contains a & b -> c.size() == 2
Thanks!
Richard -
wrote on 7 May 2012, 08:16 last edited by
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