Important: Please read the Qt Code of Conduct -

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.

  • The [url=]value()[/url] should do the trick, see canConvert() too.

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


    Then you can use QVariant::fromValuestd::vector(v); and variant.valuestd::vector();

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

Log in to reply