Hello,

How do you stream a custom QVariant? I'm getting an error when using QDataStream to output to a binary file. Error is noted in code below.

Where does the type (i.e. QVariant::Type) get specified? Is this done automatically by Q_DECLARE_METATYPE? I was trying to declare my own value (beyond QVariant::UserType), but I couldn't see where to accomplish it. It appears to just be set by defualt to QVariant::UserType, but I need multiple custom types.

I'm hoping to leverage the Qt serialization capabilities ... handling byte-order, string length, etc.

Qt Code:
  1. QDataStream & operator<<(QDataStream & out, const sStatePacket & record)
  2. {
  3. QVariant variant;
  4. variant.setValue(record);
  5. out << variant; // ERROR OCCURS HERE!!!
  6. return out;
  7. }
  8.  
  9. QDataStream & operator>>(QDataStream & in, sStatePacket & record)
  10. {
  11. QVariant variant;
  12. in >> variant;
  13. record.time = variant.value<sStatePacket>().time;
  14. record.pos = variant.value<sStatePacket>().pos;
  15. record.hpr = variant.value<sStatePacket>().hpr;
  16. return in;
  17. }
  18.  
  19. struct sStatePacket
  20. {
  21. float time;
  22. sPosRecord pos;
  23. sRotRecord hpr;
  24. };
  25. Q_DECLARE_METATYPE(sStatePacket)
To copy to clipboard, switch view to plain text mode 

Thank you,
Ben