[Qt-interest] how to output a user defined struct datatodatastream?

Vincent Cai wcai at cypress.com
Thu Oct 13 07:44:41 CEST 2011


Got it, thanks!

-----Original Message-----
From: qt-interest-bounces+wcai=cypress.com at qt.nokia.com [mailto:qt-interest-bounces+wcai=cypress.com at qt.nokia.com] On Behalf Of Scott Aron Bloom
Sent: 2011年10月13日 13:40
To: Qt-interest
Subject: Re: [Qt-interest] how to output a user defined struct datatodatastream?

Yes, in that case.. you should write out a version number as well, so you can make sure you read it into the proper structure...

Otherwise you will most likely corrupt memory.

This is pretty standard persistent data model stuff...

Scott

-----Original Message-----
From: Vincent Cai [mailto:wcai at cypress.com]
Sent: Wednesday, October 12, 2011 10:32 PM
To: Scott Aron Bloom; Samuel Gaist; Qt-interest
Subject: RE: [Qt-interest] how to output a user defined struct datatodatastream?

Thanks,
But the disadvantage is that when I change the struct definition, I need to update the operator accordingly.

-----Original Message-----
From: Scott Aron Bloom [mailto:Scott.Bloom at onshorecs.com]
Sent: 2011年10月13日 13:28
To: Vincent Cai; Samuel Gaist; Qt-interest
Subject: RE: [Qt-interest] how to output a user defined struct datato datastream?

How about

QDataStream & operator<<( QDataStream &ds, const TSA_FRAME_HEADER_STURCT & data ) {
        Ds << header << length << counter;
               For( int ii = 0; ii < 3; ++ii )
                   Ds << reserved[ ii ];
        return ds;
}

QDataStream & operator>>( QDS & ds, ... & data ) {
   Ds >> header >> length >> counter;
               For( int ii = 0; ii < 3; ++ii )
                   Ds >> reserved[ ii ];
        return ds;
}
-----Original Message-----
From: qt-interest-bounces+scott.bloom=onshorecs.com at qt.nokia.com [mailto:qt-interest-bounces+scott.bloom=onshorecs.com at qt.nokia.com] On Behalf Of Vincent Cai
Sent: Wednesday, October 12, 2011 10:22 PM
To: Samuel Gaist; Qt-interest
Subject: Re: [Qt-interest] how to output a user defined struct datato datastream?

Thanks.
        I have try below code and it can work properly, but the code seems a little weird, is there any better way to write the code?

        for (int i = 0; i < sizeof(headerInfo); ++i)
    {
        out << (quint8)(*(reinterpret_cast<const char *>(&headerInfo)+i));
    }

-----Original Message-----
From: qt-interest-bounces+wcai=cypress.com at qt.nokia.com [mailto:qt-interest-bounces+wcai=cypress.com at qt.nokia.com] On Behalf Of Samuel Gaist
Sent: 2011年10月12日 20:10
To: Qt-interest
Subject: Re: [Qt-interest] how to output a user defined struct data to datastream?

Hi,

You need to write QDataStream operators for your struct

This might be a starting point
http://doc.qt.nokia.com/4.7/qdatastream.html#reading-and-writing-other-qt-classes

Hope it helps
Samuel

On 12 oct. 2011, at 14:00, Vincent Cai wrote:

> Dear all,
>
>          How to output a user defined struct data to datastream?
>          Below code can’t pass compiling:
>
>      typedef struct
>     {
>         quint16 header;
>         quint8 length;
>         quint8 counter;
>         quint8 reserved[4];
> } TSA_FRAME_HEADER_STURCT;
>
> TSA_FRAME_HEADER_STURCT headerInfo;
>
> void TSAFrameHeader::writeHeaderString(QFile *file) {
>     QDataStream out(file);
>     out << headerInfo;
> }
>
> Thanks,
> Vincent.
>
> This message and any attachments may contain Cypress (or its subsidiaries) confidential information. If it has been received in error, please advise the sender and immediately delete this message.
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest

_______________________________________________
Qt-interest mailing list
Qt-interest at qt.nokia.com
http://lists.qt.nokia.com/mailman/listinfo/qt-interest

This message and any attachments may contain Cypress (or its subsidiaries) confidential information. If it has been received in error, please advise the sender and immediately delete this message.
_______________________________________________
Qt-interest mailing list
Qt-interest at qt.nokia.com
http://lists.qt.nokia.com/mailman/listinfo/qt-interest

This message and any attachments may contain Cypress (or its subsidiaries) confidential information. If it has been received in error, please advise the sender and immediately delete this message.
_______________________________________________
Qt-interest mailing list
Qt-interest at qt.nokia.com
http://lists.qt.nokia.com/mailman/listinfo/qt-interest

This message and any attachments may contain Cypress (or its subsidiaries) confidential information. If it has been received in error, please advise the sender and immediately delete this message.


More information about the Qt-interest-old mailing list