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. Serializing QVector of pointers
Forum Updated to NodeBB v4.3 + New Features

Serializing QVector of pointers

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 5.1k 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.
  • Y Offline
    Y Offline
    YuFr
    wrote on last edited by
    #1

    Hi,

    I'm not sure that this is a full question of Qt, but I don't know where to ask, so I am here !

    I have a QVector of objects, referenced by pointers. It is so a QVector of pointers.
    I want to serialize my class containing this QVector. I overloaded the operators << and >>, and the serializing is working for the most of my attributes.
    However, the serializing of my QVector is not working. I bet I have to serialize all the objects inside the QVector and rebuilt it in output, but I don't know how to do that. I bet I should do a deep copy of the QVector, but don't know how to proceed inside my operator << or >>.
    When I use the >> or << operator for my QVector, it serializes just the reference, and not the objects. How can I do that ?

    Thanks by advance

    1 Reply Last reply
    0
    • H Offline
      H Offline
      Hostel
      wrote on last edited by
      #2

      If you have a << and >> for your class, which is in QVector then you could write function like this:
      @
      // PSEUDOCODE - not working
      serializeVectorWithPointers()//
      {
      for (int i = 0; i < vector.size(); ++i)
      {
      output << *( vector.at( i ) );
      }
      }
      @

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        Did you implement operator<< and operator>> for the pointer type of your type?

        @
        QDataStream& operator<<(QDataStream& s, const YourType *yt);
        QDataStream& operator>>(QDataStream& s, YourType *yt);
        @

        And don't forget to make operator<< a friend of your class, otherwise you cannot access the private members you want to serialize.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          I would start looking into other solutions as well. Boost::serialize for instance can serialize pointers to objects, and makes sure that you don't serialize the same object more than one time.

          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