1

When I write raw network bytes to a QDataStream object repeatedly, the QDataStream never increases the write position of the underlying QByteArray.

Here's the code:

QByteArray recvBuf;
int bytePosition = 0;

void init( ){
    recvBuf = QByteArray( 240 * 10, Qt::Uninitiliazed );
}

void receiveAppend( ){    
    QByteArray buffer( 240, Qt::Uninitialized );
    QDataStream datastream( recvBuf, QIODevice::WriteOnly );
    datastream.setByteOrder( QDataStream::LittleEndian );

    if( udpSocket->readDataGram( buffer.data(), buffer.size(), &ip, &port ){
        datastream.device().seek( bytePosition );
        datastream.writeRawData( buffer.data(), buffer.size() );
        bytePosition += 240;
    }
}

Why is this happening?

The code works fine on Linux Mint 18 64Bit. But it's not working on Windows 10 64Bit.
I'm using Qt Creator 4.3.1 with Qt 5.9.1.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
chris-kuhr
  • 355
  • 1
  • 5
  • 19
  • Why do you think you need (or want) to seek within the `QIODevice` whilst using `QDataStream`? Just let the `QDataStream` do its thing as per the [documentation](http://doc.qt.io/qt-5/qdatastream.html). Sorry if I've misunderstood the problem. – G.M. Sep 28 '17 at 18:05
  • On the Linux machine it works with or without seeking the position, while on Win10 both attempts do not work. The seek call was a try to get it working on Win eather without success. Why are those functions behaving different on Win/Linux? – chris-kuhr Sep 29 '17 at 06:10
  • Yes, seeking is only needed when you did manipulations on your `QIODevice` directly, so only when changing the position of the stream. – jaques-sam Jan 16 '19 at 08:29

0 Answers0