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

Scott Aron Bloom Scott.Bloom at onshorecs.com
Thu Oct 13 07:27:55 CEST 2011


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



More information about the Qt-interest-old mailing list