You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

  • "pmouraguedes" is male
  • "pmouraguedes" started this thread

Posts: 31

Location: Portugal

  • Send private message

1

Friday, October 1st 2004, 5:58am

QDataStream

Hi,

I have this:

Source code

1
2
3
4
5
6
    [...]
    QByteArray m_array;     
    QDataStream s(m_array, IO_WriteOnly);
    s << doc.toString();
    cout << doc.toString() << endl;
    cout << QString(m_array) << endl;


The first cout prints a non empty string but the second (the QByteArray one) prints an empty string. Am I missing something?

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

2

Friday, October 1st 2004, 10:42am

RE: QDataStream

Quoted

Originally posted by pmouraguedes
The first cout prints a non empty string but the second (the QByteArray one) prints an empty string. Am I missing something?

Yes, you are missing that you are writing into a QDataStream. a QTextStream creates text, a QDataStream binary data.
For example when writing a QString into a QDataStream the information in the byte array buffer also include information about the type (QString) and about length, etc

Cheers,
_
Qt/KDE Developer
Debian User

  • "pmouraguedes" is male
  • "pmouraguedes" started this thread

Posts: 31

Location: Portugal

  • Send private message

3

Friday, October 1st 2004, 12:52pm

RE: QDataStream

But it shouldn't print something in the second cout?
It seems that m_array is empty as it cames to the world :)

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

4

Friday, October 1st 2004, 1:13pm

RE: QDataStream

Quoted

Originally posted by pmouraguedes
But it shouldn't print something in the second cout?

Not necessarily.
For example assume that the first byte is some unprintable character and the second byte is 0.
Then QString's constructor interprets this as a string of length 1 (one character before 0-terminator) and you don't see anything because the only character of the string is not printable.

Quoted


It seems that m_array is empty as it cames to the world :)

Easily checkable by printing the value returned by the byte arrays size() method :)
If it is 0 you might have to close or flush the stream.

Cheers,
_
Qt/KDE Developer
Debian User