Results 1 to 5 of 5

Thread: Convert QByteArray to object and vice versa

  1. #1
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Convert QByteArray to object and vice versa

    I have a data structure:

    Qt Code:
    1. typedef struct
    2. {
    3. QString IpAddress;
    4. int Port;
    5. } VideoSource;
    6.  
    7. 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:

    Qt Code:
    1. int videoSourceMetaTypeId = qRegisterMetaType< VideoSource >();
    2. ...
    3. ...
    4. VideoSource vs;
    5. vs.IpAddress = "127.0.0.1";
    6. vs.Port = 5678;
    7. QVariant videoSourceAsVariant;
    8. videoSourceAsVariant.setValue( vs );
    9. QByteArray videoSourceBytes = videoSourceAsVariant.toByteArray();
    10. 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...

    Qt Code:
    1. QVariant testVar( videoSourceBytes );
    2. if( testVar.canConvert< VideoSource >() ) // returns false
    3. {
    4. VideoSource testVs = testVar.value< VideoSource >();
    5. }
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to DiamonDogX for this useful post:


  3. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Convert QByteArray to object and vice versa

    It's because QVariant doesn't know how to convert your object to byte array and as far as I remember there is no way to teach it do it.

    What you should do is reimplement operators (<< and >>) for QDataStream to be able to stream your object into a datastream that can operate on a byte array.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #3
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Convert QByteArray to object and vice versa

    Ok. I am going to work on that. Do you know why this doesn't compile?

    VideoSource.h:

    Qt Code:
    1. #ifndef VideoSource_h_
    2. #define VideoSource_h_
    3.  
    4. #include <QString>
    5. #include <QDataStream>
    6.  
    7. #pragma pack 1
    8. typedef struct
    9. {
    10. QString IpAddress;
    11. quint16 Port;
    12. } VideoSource;
    13.  
    14. QDataStream & operator<< ( QDataStream & stream, const VideoSource & vs );
    15. QDataStream & operator>> ( QDataStream & stream, VideoSource & vs );
    16.  
    17. Q_DECLARE_METATYPE( VideoSource )
    18.  
    19. #endif
    To copy to clipboard, switch view to plain text mode 


    VideoSource.cpp:

    Qt Code:
    1. #include "VideoSource.h"
    2.  
    3. QDataStream & operator<< ( QDataStream & stream, const VideoSource & vs )
    4. {
    5. return stream;
    6. }
    7.  
    8. QDataStream & operator>> ( QDataStream & stream, VideoSource & vs )
    9. {
    10. return stream;
    11. }
    To copy to clipboard, switch view to plain text mode 

    I get errors like:
    Error 2 error C3646: 'QDataStream' : unknown override specifier
    Error 3 error C2143: syntax error : missing ';' before '&'
    Error 4 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    Error 5 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    Error 6 error C2556: 'int &operator <<(QDataStream &,const VideoSource &)' : overloaded function differs only by return type from 'QDataStream &operator <<(QDataStream &,const VideoSource &)'
    Error 7 error C2371: 'operator <<' : redefinition; different basic types
    Last edited by DiamonDogX; 20th July 2009 at 19:50.

  5. #4
    Join Date
    Apr 2008
    Location
    Pittsburgh,PA,USA
    Posts
    48
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Convert QByteArray to object and vice versa

    Looks like you need:

    #include <QDataStream>

    [EDIT]

    I see you have that in your header file. [/EDIT]

    [EDIT2] Whats with the ; on line 9 of your header file [EDIT2]
    Last edited by drescherjm; 20th July 2009 at 19:32.
    John

  6. #5
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Convert QByteArray to object and vice versa

    Took out the following line and it worked:

    Q_DECLARE_METATYPE( VideoSource )

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.