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.

1

Tuesday, May 8th 2012, 3:47pm

Send object with QByteArray

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

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
 
 }

2

Tuesday, May 8th 2012, 6:53pm

you can't simply expect that your class will convert into char* and be able to send it. If you could, there would be no need for serialisation, e.g. boost serialisation.

only POD types can be 'easily'* converted to byte array.


* and even if you can easily convert it, there is no guarantee that another computer will convert it back in the desired way.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.