Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Solved Serialising variants - QVector<int>() from value.

    General and Desktop
    qvariant serialization serialize
    1
    1
    528
    Loading More Posts
    • 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.
    • D
      Dariusz last edited by Dariusz

      Hey

      And I'm stuck, I have a

      QMap<int,QMap<int,QVariant>> as my data structure in my treeView.
      

      I was trying to serialize it but I keep getting >

      WARNING: QVariant::save: unable to save type 'QVector<int>' (type id: 1033).
      
       (kernel\qvariant.cpp:2593, void __cdecl QVariant::save(class QDataStream &) const)
      

      As far as I can tell, I have 7 items in my map and some of them are :

      QVector<int> data;
      QVariant::fromValue(data);
      

      Other than that I don't see other issues, when I look in debug at the map some of variatns have no value at all and others have (invalid) as flag next to them...

      Mhmhmmhmmm....

      What did I do ? :- )


      Kay so I did some digging, looks like I have to register it and somehow implement...but how?

          qRegisterMetaType<QVector<int>>("QVector<int>");
      
          QMap<int, QVariant> wah;
          wah[0] = QVariant::fromValue(QVector<int>{12, 231, 45, 125, 123, 12, 312, 4, 12});
          qDebug() << wah;
      
          QByteArray ar;
          QDataStream s(&ar, QIODevice::WriteOnly);
          s << wah;
      

      returns the same error...


      Tried adding this above main... still no luck...

      QDataStream &operator<<(QDataStream &out, const QVector<int> &vec) {
          out << vec.size();
          for (auto &v:vec) {
              out << v;
          }
          return out;
      };
      
      
      int main(int argc, char *argv[]) {
      

      Answer :

      Needed to add :

      qRegisterMetaTypeStreamOperators<QVector<int>>("QVector<int>");
      

      I really wish that Docs would explain that... "easier" or mention it somehow. Took a while to read a whole bunch of info to realize this :/

      1 Reply Last reply Reply Quote 0
      • First post
        Last post