Results 1 to 7 of 7

Thread: Cant 'connect' an internal char array to a qdatastream

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Cant 'connect' an internal char array to a qdatastream

    I have :
    Qt Code:
    1. mybuffer = new char[265536]();
    2.  
    3. QByteArray data = QByteArray::fromRawData(mybuffer, sizeof(mybuffer));
    4. the_qstream = new QDataStream(&data , QIODevice::ReadWrite);
    5. the_qstream->setVersion(QDataStream::Qt_4_6);
    6. QDebug()<<" size buffer"<< the_qstream->device()->size();
    To copy to clipboard, switch view to plain text mode 

    Ok, first I have 4 for the size buffer ???
    And the_qstream<<a_float; fails at
    inline int QByteArray::size() const { return d->size; }

    Any help ? What am I doing bad ?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Cant 'connect' an internal char array to a qdatastream

    myBuffer is a pointer to an array of 265536 chars. sizeof( char * ) == 4 on your system.

    And you are setting the QDataStream with a QByteArray, not a QIODevice, so the call to QDataStream::device() returns a NULL pointer. Read the docs!
    Last edited by d_stranz; 17th November 2011 at 23:19.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Cant 'connect' an internal char array to a qdatastream

    Actually QDataStream::device() returns a pointer to the internally created QBuffer in this case. A quick test has size() returning the correct value, just not what Tonnot is expecting, and no issue streaming a float.
    Qt Code:
    1. float a_float = 0.0;
    2. QDataStream stream(&buf, QIODevice::WriteOnly);
    3. stream << qint32(0) << a_float;
    4. qDebug() << stream.device() << stream.device()->size();
    5. // Outputs: QBuffer(0x2533720) 12
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cant 'connect' an internal char array to a qdatastream

    Finally this code works :
    qbytearray = new QByteArray(265536,0);
    the_qstream = new QDataStream(qbytearray , QIODevice::ReadWrite);
    the_qstream->setVersion(QDataStream::Qt_4_6);
    the_qstream->setFloatingPointPrecision(QDataStream:oublePrecision);
    the_qstream->setByteOrder(QDataStream::LittleEndian);

    I'm going to use liitteendian, because I think my 80% of users are going to be windows- 'pc' users.

    A last question, using qdatastream, writing a float with 4, 8 for double, using litteendian, are going to means a file with 12 bytes. Knowing this information, the data coulde be recovered using another languaje ins't ?. Or could not ?
    Thanks

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Cant 'connect' an internal char array to a qdatastream

    If the receiving program is not a Qt program then you need to be careful that you stream only intrinsic types or the data received may not be what you expect. For example streaming a QByteArray containing 10 bytes to a QDataStream will generate a 14 byte stream, the C-string "Hello" generates another 10.

    As for using another language's integer type, well, that might work if the integers are the same size and endian-ness. Floating point will only be binary compatible if the two ends use the same encoding, usually IEEE 754, and the same variant where several exist. Four-byte floats may not exist in some languages.

  6. #6
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cant 'connect' an internal char array to a qdatastream

    By now, I'm talking at file level (I also am uising the data at memory level)
    The need of QTdatastream for reading the data could be interesting for me. That is, it would be very difficult to read my file.

    But In case of need, I could document my data structure ? :
    "read 4 bytes : reserved
    read 10 : whatever
    read 4 : float
    read 8 : double
    etc...
    Endian : liitle-endian."

    Thanks Chrish

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Cant 'connect' an internal char array to a qdatastream

    Quote Originally Posted by ChrisW67 View Post
    Actually QDataStream::device() returns a pointer to the internally created QBuffer in this case. A quick test has size() returning the correct value, just not what Tonnot is expecting, and no issue streaming a float.
    I believe you, but the documentation is misleading:

    QIODevice * QDataStream::device () const
    Returns the I/O device currently set, or 0 if no device is currently set.
    The QByteArray form of the constructor does say that an internal QBuffer will be created, but it isn't obvious that this would be returned as the QIODevice pointer. So apparently only in the case of an instance created through the default constructor will the QIODevice pointer be null.

Similar Threads

  1. Char array[6] to QString and display it
    By arunvv in forum Newbie
    Replies: 11
    Last Post: 12th March 2014, 20:48
  2. Replies: 6
    Last Post: 16th July 2010, 11:07
  3. Converting QString to char array
    By srohit24 in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2009, 18:19
  4. Conversion Char Array to string
    By anafor2004 in forum Newbie
    Replies: 6
    Last Post: 6th May 2008, 14:35
  5. Char Array to QString
    By 3nc31 in forum Qt Programming
    Replies: 2
    Last Post: 25th November 2007, 22:18

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.