Results 1 to 11 of 11

Thread: QDataStream question...

  1. #1
    Join Date
    Jul 2007
    Posts
    27
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QDataStream question...

    Is it permissible to mix serialization of data and readRawData/writeRawData to a QDataStream?

    In other words, imagine I have information stored in a class that I'd like to transfer over the network using QDataStream, and imagine I have a pointer to that object:

    Qt Code:
    1. myClass *myObject = new myClass;
    To copy to clipboard, switch view to plain text mode 

    Would it be OK to do something like this:

    Qt Code:
    1. ... // assume 'out' is a QDataStream object.
    2. out << (quint16)0; // Initialize "bytes of data" to 0
    3. out.writeRawData((char *)myObject, myObject->sizeInBytes()); // put binary data into stream
    4. out.device()->seek(0); // seek back to beginning of stream
    5. out << (quint16)(out.size() - sizeof(quint16)); // set the "bytes of data"
    To copy to clipboard, switch view to plain text mode 

    Or would it confuse Qt?

    Same question for input of binary data using readRawData() ...

    Thanks,

    Gordon E.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QDataStream question...

    Qt won't mind, but you might be unable to retrieve the object back on the other side of the connection. Can't you do the proper serialization?

  3. The following user says thank you to wysota for this useful post:

    grellsworth (19th April 2008)

  4. #3
    Join Date
    Jul 2007
    Posts
    27
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream question...

    Quote Originally Posted by wysota View Post
    Qt won't mind, but you might be unable to retrieve the object back on the other side of the connection.
    Why do you say that?
    Can't you do the proper serialization?
    You mean something like this:
    Qt Code:
    1. out << (char *)myObject;
    To copy to clipboard, switch view to plain text mode 

    ???

    I didn't think that would work.

    I don't know... do you have any other suggestions for transferring that data?

    Gordon E.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QDataStream question...

    Quote Originally Posted by grellsworth View Post
    Why do you say that?
    Because the application running on the other end of connection might be compiled with a different compiler and/or for a different platform (including 32/64 bit and little/big endian differences).

    You mean something like this:
    Qt Code:
    1. out << (char *)myObject;
    To copy to clipboard, switch view to plain text mode 

    ???
    No, I mean serialize.

    I don't know what your object consists of, but you need to represent it with a string. For instance:

    Qt Code:
    1. struct MyStruct {
    2. int x;
    3. int y;
    4. QString txt;
    5. QDataStream &operator<<(QDataStream &stream) const {
    6. stream << x << y << txt;
    7. return stream;
    8. }
    9. QDataStream &operator>>(QDataStream &stream){
    10. stream >> x >> y >> txt;
    11. return stream;
    12. }
    13. };
    To copy to clipboard, switch view to plain text mode 

    The last two operators should allow you to serialize this class' instances using QDataStream.

  6. The following user says thank you to wysota for this useful post:

    grellsworth (20th April 2008)

  7. #5
    Join Date
    Jul 2007
    Posts
    27
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: QDataStream question...

    Quote Originally Posted by wysota View Post
    Because the application running on the other end of connection might be compiled with a different compiler and/or for a different platform (including 32/64 bit and little/big endian differences).


    No, I mean serialize.

    I don't know what your object consists of, but you need to represent it with a string. For instance:

    Qt Code:
    1. struct MyStruct {
    2. int x;
    3. int y;
    4. QString txt;
    5. QDataStream &operator<<(QDataStream &stream) const {
    6. stream << x << y << txt;
    7. return stream;
    8. }
    9. QDataStream &operator>>(QDataStream &stream){
    10. stream >> x >> y >> txt;
    11. return stream;
    12. }
    13. };
    To copy to clipboard, switch view to plain text mode 

    The last two operators should allow you to serialize this class' instances using QDataStream.
    Ahh, I agree with you about the first part... That's a good point.

    The way you've got it to serialize would work pretty well, too... but it would be tedious if a class contained a lot of members (say 100 or more). It's definitely something to think about, thank you very much for your input.

    I'm just curious, if I did the serialization operators as you recommend, and then include this class as a member of another class (which also has serialization operators), would the parent class serialize the child correctly?

    (I know I'm using parent/child incorrectly here, usually we only use those terms for classes that inherit other classes, but I'm not sure what other term to use.)

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QDataStream question...

    Quote Originally Posted by grellsworth View Post
    I'm just curious, if I did the serialization operators as you recommend, and then include this class as a member of another class (which also has serialization operators), would the parent class serialize the child correctly?
    Yes, of course. Just as I'm serializing QString in the example above although I did not implement redirect operators for it myself.

  9. #7
    Join Date
    Mar 2006
    Posts
    140
    Thanks
    8
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream question...

    The way you've got it to serialize would work pretty well, too... but it would be tedious if a class contained a lot of members (say 100 or more).
    Something to consider would be a code generator of some sort.
    I personally use "CodeSmith Tools".
    In that you could list all your members in a flat text file, then write a small amount of CodeSmith scripting to dump a Class with your << and >> all filled out and ready to go.

    Actualyl it's something I've been meaning to do for ages. I'll whip up a template tonight and upload it to the forum.

  10. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QDataStream question...

    You could enter the contest with it.

  11. #9
    Join Date
    Mar 2006
    Posts
    140
    Thanks
    8
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream question...

    Maybe, although the Image Format one that's almost complete might be a better candidate.

  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QDataStream question...

    Who stops you from entering in more than one category?

  13. #11
    Join Date
    Mar 2006
    Posts
    140
    Thanks
    8
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream question...

    Here's the first cut. I wrote this with CodeSmith Tools 4.1
    Get it from http://www.codesmithtools.com/
    It requires .NET 2 (sorry to linux only people but hey, it was the quickest way to get the job done).

    The process is:
    1. Install CodeSmith Tools.
    2. Open the .cst file I've provided in CodeSmith Tools.
    3. "Build" the template. A properties list will appear to the right.
    4. There should be some default values, if you want a class, set the "GenerateClass" property to True.
    5. Set the name for the struct / class
    6. The struct / class will have members set based on the content of the "SourceFile" property value. Ie, in the Members.txt file is list of members and their types in this format <Type>,<Name>. Follow the format shown for your own members.
    7. Press F5 to run the template.
    8. Copy and paste the generated code into your source file in your IDE / text editor.

    Let me know if you want anything added.
    This is totally free, do whatever you want with it, or ask for additions and I'll provide if I can.

    Stevey
    Attached Files Attached Files
    Last edited by stevey; 21st April 2008 at 13:23.

Similar Threads

  1. SQL Question
    By ^NyAw^ in forum Qt Programming
    Replies: 5
    Last Post: 8th April 2008, 20:36
  2. QDataStream use
    By Doug Broadwell in forum Qt Programming
    Replies: 2
    Last Post: 21st July 2007, 23:17
  3. Question regarding how to paint after zoom.
    By JonathanForQT4 in forum Qt Programming
    Replies: 2
    Last Post: 26th January 2007, 16:34
  4. QDataStream >> QString
    By smalls in forum Qt Programming
    Replies: 2
    Last Post: 17th January 2006, 23:14

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.