[Qt-interest] Transferring a QPixmap through QTcpSocket

Bo Thorsen bo at fioniasoftware.dk
Thu Apr 7 13:21:16 CEST 2011


Den 07-04-2011 13:00, Joshua W Joseph skrev:
> Hi All,
>
> I have two simple applications which are network based. The 'server'
> takes a screenshot of its computer. When a 'client' connects, it will
> receive the captured image. These are basically the fortune
> server/client examples modified to transmit images instead of text.
>
> Here is my code for sending the image:
>
>        QByteArrayblock;
>
>      QDataStream  out(&block,  QIODevice::WriteOnly);
>
>      out.setVersion(QDataStream::Qt_4_0);
>
>      out  <<  (quint16)0;
>
>      out  <<  originalPixmap;
>
>      qDebug()  <<  "Pixmap  Sent";
>
>      out.device()->seek(0);
>
>      out  <<  (quint16)(block.size()  -  sizeof(quint16));
>
>      QTcpSocket  *clientConnection  =  tcpServer->nextPendingConnection();
>
>      connect(clientConnection,  SIGNAL(disconnected()),
>
>              clientConnection,  SLOT(deleteLater()));
>
>      clientConnection->write(block);
>
>      clientConnection->disconnectFromHost();
>
>
> And here is my code for reading it
>
>       QDataStreamin(tcpSocket);
>
>      in.setVersion(QDataStream::Qt_4_0);
>
>      if  (blockSize  ==  0)  {
>
>      if  (tcpSocket->bytesAvailable()  <  (int)sizeof(quint64))
>
>              return;
>
>          in  >>  blockSize;
>
>      }
>
>      if  (tcpSocket->bytesAvailable()  <  blockSize)
>
>          return;
>
>      QPixmap  nextPixmap;
>
>      in  >>  nextPixmap;
>
>      originalPixmap  =  currentPixmap;
>
>      updateScreenshotLabel();
>
>      getFortuneButton->setEnabled(true);
>
>      received  =  false;
>
>
>
> The client receives some data, but how to I:
>      1. Ensure that all of the sent data is received, and
>      2. Convert the received data into a QPixmap so that it can be
> displayed.
>
> I know it sounds simple but i have racked  my brains for several days
> now and I just cant seem to get it. Please assist.

Qt does not create the protocol for you, it only gives you the network 
and marshalling tools. You have to write something for this yourself. 
For example sending an int with the number of bytes for the image before 
sending the actual image.

Bo Thorsen,
Fionia Software.

-- 

Expert Qt and C++ developer for hire
Contact me if you need expert Qt help
http://www.fioniasoftware.dk



More information about the Qt-interest-old mailing list