PDA

View Full Version : send custom object with qbytearray



kevindebb
8th May 2012, 16:28
Hello everybody,

I would like to transfer a custom object with qbytearray. I tried to send "TetrixShape board[BoardWidth * BoardHeight];" This is a custom object.
I would think that if you append this to qbytearray that this object will be converted to a bytearray. But this doesn't happen. I get the following error: invalid conversion from 'TetrixShape*' to 'char' and

Can somebody help me?

Thanks



TetrixShape board[BoardWidth * BoardHeight];


...


QByteArray data;
data.append(board);
emit send_board(board);

...


void server::send_data(QByteArray data)
{
QDataStream out(&data, QIODevice::WriteOnly);
out << (quint16)0;
out << data;
out.device()->seek(0);
out << (quint16)(data.size() - sizeof(quint16));
connection->write(data);
qDebug()<<data;
}

amleto
8th May 2012, 18:57
oh god, another one that likes cross posting.

http://www.qtforum.org/article/37901/send-object-with-qbytearray.html

kevindebb
8th May 2012, 21:32
Sorry, I was a little impatient. :rolleyes:

ChrisW67
9th May 2012, 12:16
There is no magic in programming: you cannot convert complex data structures into simple byte streams without some effort.
You also cannot do it without understanding some basic C++.

You can use QDataStream to create a byte array that can be read at the other end (assuming it is a Qt program) but your send_data(), or should that be send_board(), routine is horribly confused. It creates a local copy of the byte array passed in {14}, then opens a stream on that copy {16}, tries to write itself on that stream {18}, updates some byte counts and then sends it. I am not sure what the result will be (4 bytes?) but I expect it is not pretty and certainly not what you expect.