Results 1 to 4 of 4

Thread: send custom object with qbytearray

  1. #1

    Default send custom 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

    Qt Code:
    1. TetrixShape board[BoardWidth * BoardHeight];
    2.  
    3.  
    4. ...
    5.  
    6.  
    7. data.append(board);
    8. emit send_board(board);
    9.  
    10. ...
    11.  
    12.  
    13. void server::send_data(QByteArray data)
    14. {
    15. QDataStream out(&data, QIODevice::WriteOnly);
    16. out << (quint16)0;
    17. out << data;
    18. out.device()->seek(0);
    19. out << (quint16)(data.size() - sizeof(quint16));
    20. connection->write(data);
    21. qDebug()<<data;
    22. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by kevindebb; 8th May 2012 at 17:03.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: send custom object with qbytearray

    oh god, another one that likes cross posting.

    http://www.qtforum.org/article/37901...bytearray.html
    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.

  3. #3

    Default Re: send custom object with qbytearray

    Sorry, I was a little impatient.

  4. #4
    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: send custom object with qbytearray

    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.

Similar Threads

  1. Replies: 0
    Last Post: 26th April 2011, 11:02
  2. Replies: 2
    Last Post: 5th October 2010, 07:07
  3. Can you add a custom object to a SQL database?
    By agerlach in forum Qt Programming
    Replies: 4
    Last Post: 30th June 2010, 15:51
  4. Convert QByteArray to object and vice versa
    By DiamonDogX in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2009, 20:07
  5. Replies: 3
    Last Post: 14th October 2008, 21:04

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
  •  
Qt is a trademark of The Qt Company.