I'm writing a simple TCP based network application in Qt and wanted to use QDataStream
and QByteArray
to send data over the network. The problem is that when I'm putting data into QByteArray
they are "zeroed". For example (a slot in MainWindow
that is connected to timer timeout signal):
void MainWindow::SendPlayer1Data(){
QByteArray block;
QDataStream s(&block, QIODevice::OpenModeFlag::ReadWrite);
QString h="hello";
s<<h;
qDebug() << "h: " << data;
qDebug() << "block: " << QString(block); // equivalent to s >> h on receiving end
qDebug() << "block[0]: " << int(block[0]);
}
h: "hello"
block: ""
block[0]: 0
I receive "hello"
once at the beginning but after that I only get ""
. The same goes for qint32
. Both client and server shows that QByteArray
size is 14 bytes, so QDataStream
writes data into that array, but it makes them 0
(it shows ""
when I use s >> h
and then use qDebug() << h
)