I have a data structure:
typedef struct
{
int Port;
} VideoSource;
Q_DECLARE_METATYPE( VideoSource )
typedef struct
{
QString IpAddress;
int Port;
} VideoSource;
Q_DECLARE_METATYPE( VideoSource )
To copy to clipboard, switch view to plain text mode
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;
videoSourceAsVariant.setValue( vs );
QByteArray videoSourceBytes
= videoSourceAsVariant.
toByteArray();
int numBytes = videoSourceBytes.count(); // this is 0
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
To copy to clipboard, switch view to plain text mode
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...
if( testVar.canConvert< VideoSource >() ) // returns false
{
VideoSource testVs = testVar.value< VideoSource >();
}
QVariant testVar( videoSourceBytes );
if( testVar.canConvert< VideoSource >() ) // returns false
{
VideoSource testVs = testVar.value< VideoSource >();
}
To copy to clipboard, switch view to plain text mode
Bookmarks