qtcpsocket send and recieve image

点击打开链接
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:

      QByteArray block; 

    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

     QDataStream in(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.

Thank you in advance,

Joshua W.

 

I am getting these errors:
QPixmap::scaled: Pixmap is a null pixmap 

QSize(-1, -1) 

posted @ 2011-11-21 09:38  Podevor  阅读(337)  评论(0编辑  收藏  举报