Results 1 to 12 of 12

Thread: Copy with QDataStream

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Copy with QDataStream

    you can assume that you size can be stored in 32-bit (4 byte) long int - you can use qint32 (or even quint32 - size can't be less then 0).
    so you have to convert quint32 into QByteArray, writing there byte by byte of your quint32, something like this:
    Qt Code:
    1. quint32 size = ...; // your size
    2. QByteArray intBytes;
    3. for (int i = 0; i < 4; ++i) {
    4. quint8 b = (size >> (i*8)) & 0x000000ff;
    5. intBytes.append(b);
    6. }
    To copy to clipboard, switch view to plain text mode 
    on the client side you have to create a valid quint32 from a byte array:
    Qt Code:
    1. QByteArray intBytes = socket.read(4); // 4*8 = 32 bits
    2. quint32 size = 0; // init with 0 to be sure there is really 0
    3. for (int i = 0; i < 4; ++i) {
    4. quint8 b = intBytes.at(i);
    5. size |= (((quint32)b) & 0x000000ff) << i*8;
    6. }
    To copy to clipboard, switch view to plain text mode 
    (should work but i did not check so try to print the int first to see if it is what you have sent :])
    now you have the size, so whenever readyRead() comes from socket you have to check int the slot if you have to start reading new image (read size and some of data) or you are in the middle of image data. Whenever you read image data just check if bytesAvailable() are equal to the needed size, if less than readAll() and decrease size by a number of bytes you already read. and so on... In other words reading data becomes a little state machine :] so you have to react adequately to the state you are in.

    P.S. And it is nice to check if file was opened succesfully, so instead of this:
    Qt Code:
    1. file.open(QIODevice::ReadOnly);
    To copy to clipboard, switch view to plain text mode 
    better do it like this:
    Qt Code:
    1. if (!file.open(QIODevice::ReadOnly)) {
    2. // do something proper if cannot open file, e.g.:
    3. qDebug("Cannot open file!");
    4. return;
    5. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by faldzip; 9th July 2009 at 12:53. Reason: updated contents
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

Similar Threads

  1. Replies: 1
    Last Post: 27th November 2014, 09:11
  2. Q_INTERFACES with classes having a copy constructor
    By Mike in forum Qt Programming
    Replies: 0
    Last Post: 31st October 2008, 12:40
  3. QObject and copy Constructors
    By December in forum Qt Programming
    Replies: 5
    Last Post: 17th July 2008, 16:14
  4. copy and run a .exe file in another system
    By sabeesh in forum Installation and Deployment
    Replies: 3
    Last Post: 22nd August 2007, 10:05
  5. QDataStream >> QString
    By smalls in forum Qt Programming
    Replies: 2
    Last Post: 17th January 2006, 22:14

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.