DiamonDogX
20th July 2009, 17:14
I have a data structure:
typedef struct
{
QString IpAddress;
int Port;
} VideoSource;
Q_DECLARE_METATYPE( VideoSource )
All I want to do is be able to convert this to a byte array in order to send on a socket, and get the object back (deserialize) on the other end. So I am doing this:
int videoSourceMetaTypeId = qRegisterMetaType< VideoSource >();
...
...
VideoSource vs;
vs.IpAddress = "127.0.0.1";
vs.Port = 5678;
QVariant videoSourceAsVariant;
videoSourceAsVariant.setValue( vs );
QByteArray videoSourceBytes = videoSourceAsVariant.toByteArray();
int numBytes = videoSourceBytes.count(); // this is 0
Not sure why the byte array's size ends up being 0.
So how do I get the object back? Was trying this but obviously it's wrong...
QVariant testVar( videoSourceBytes );
if( testVar.canConvert< VideoSource >() ) // returns false
{
VideoSource testVs = testVar.value< VideoSource >();
}
typedef struct
{
QString IpAddress;
int Port;
} VideoSource;
Q_DECLARE_METATYPE( VideoSource )
All I want to do is be able to convert this to a byte array in order to send on a socket, and get the object back (deserialize) on the other end. So I am doing this:
int videoSourceMetaTypeId = qRegisterMetaType< VideoSource >();
...
...
VideoSource vs;
vs.IpAddress = "127.0.0.1";
vs.Port = 5678;
QVariant videoSourceAsVariant;
videoSourceAsVariant.setValue( vs );
QByteArray videoSourceBytes = videoSourceAsVariant.toByteArray();
int numBytes = videoSourceBytes.count(); // this is 0
Not sure why the byte array's size ends up being 0.
So how do I get the object back? Was trying this but obviously it's wrong...
QVariant testVar( videoSourceBytes );
if( testVar.canConvert< VideoSource >() ) // returns false
{
VideoSource testVs = testVar.value< VideoSource >();
}