Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to send QPixmap using QTcpSocket?



  • Hello,
    I have client and server app. I would like to send QPixmap from client to server.

    My client code:

        auto pixmap = qApp->screens().at(0)->grabWindow(QDesktopWidget().winId());
        QByteArray byte;
        QBuffer buffer(&byte);
        buffer.open(QIODevice::WriteOnly);
        pixmap.save(&buffer, "PNG");
        qInfo()<<tcp->write(buffer.data());
        qInfo()<<tcp->errorString();
    

    This code above I execute every 1 seconds ( QTimer and timeout() signal ).

    And my server code:
    In newConnection slot:

    qInfo()<<"new Connection";
    QTcpSocket *socket = server->nextPendingConnection();   /// server is QTcpServer
    QByteArray byte = socket->readAll();
    qInfo()<<byte.size();
    QPixmap pixmap;
    if(pixmap.loadFrodData(byte, "PNG"))
    {
         label->setPixmap(pixmap);
    }
    socket->close();
    delete socket;
    

    On my server I see only one textes ( not every second - only once )
    "new Connection"
    0

    On my client I see:
    numbers like 380124
    "Unknown error"

    And I see that textes every 1 second


  • Lifetime Qt Champion

    Hi,

    You are closing the socket immediately so unless you reopen the connection each time, the error you see is normal.

    Next, you are using wrong expectations with your code. Unless your image is really small, you won't have all the data in one go like that on the other side.

    Take a look at the transaction part of a QDataStream.


Log in to reply