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.
{
variant.setValue(record);
out << variant; // ERROR OCCURS HERE!!!
return out;
}
{
in >> variant;
record.time = variant.value<sStatePacket>().time;
record.pos = variant.value<sStatePacket>().pos;
record.hpr = variant.value<sStatePacket>().hpr;
return in;
}
struct sStatePacket
{
float time;
sPosRecord pos;
sRotRecord hpr;
};
Q_DECLARE_METATYPE(sStatePacket)
QDataStream & operator<<(QDataStream & out, const sStatePacket & record)
{
QVariant variant;
variant.setValue(record);
out << variant; // ERROR OCCURS HERE!!!
return out;
}
QDataStream & operator>>(QDataStream & in, sStatePacket & record)
{
QVariant variant;
in >> variant;
record.time = variant.value<sStatePacket>().time;
record.pos = variant.value<sStatePacket>().pos;
record.hpr = variant.value<sStatePacket>().hpr;
return in;
}
struct sStatePacket
{
float time;
sPosRecord pos;
sRotRecord hpr;
};
Q_DECLARE_METATYPE(sStatePacket)
To copy to clipboard, switch view to plain text mode
Thank you,
Ben
Bookmarks