July 20, 2010

PJB0515 PJB0515
Lab Rat
1 posts

QVariant to std::vector

 

Is it possible to convert from a QVariant to a std::vector? Basically I wrapped a MyStruct up in a QVariant, this went all good, but MyStruct holds a vector of MyStruct2s. So in my QTableModel, i’m trying to access that vector via the overridden data() function, but I am not sure how to get it back into the vector state.

2 replies

July 20, 2010

Zlatomir Zlatomir
Hobby Entomologist
366 posts

The value() should do the trick, see canConvert() too.

July 21, 2010

Olivier Goffart Olivier Goffart
Lab Rat
21 posts

QVariant can contains std::vector, you just need to do somewhere

Q_DECLARE_METATYPE(std::vector);

Then you can use QVariant::fromValue<std::vector>(v); and variant.value<std::vector>();

But you may want to convert to QVector before with QVector::from/toStdVector(). This really depends of your use case.

 
  ‹‹ Using ’0’ instead of ’NULL’      QImage load JPEGs not working - Qt4.6.3, QtCreator2.0, Ubuntu 9.8 ››

You must log in to post a reply. Not a member yet? Register here!