Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QVariant to std::vector
Forum Updated to NodeBB v4.3 + New Features

QVariant to std::vector

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 7.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PJB0515
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • ZlatomirZ Offline
      ZlatomirZ Offline
      Zlatomir
      wrote on last edited by
      #2

      The [url=http://doc.trolltech.com/4.6/qvariant.html#value]value()[/url] should do the trick, see canConvert() too.

      https://forum.qt.io/category/41/romanian

      1 Reply Last reply
      0
      • O Offline
        O Offline
        ogoffart
        wrote on last edited by
        #3

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

        Q_DECLARE_METATYPE(std::vector);

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved