Nominate our 2022 Qt Champions!

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);;, "PNG");

    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();
    QPixmap pixmap;
    if(pixmap.loadFrodData(byte, "PNG"))
    delete socket;

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

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

    And I see that textes every 1 second

  • Lifetime Qt Champion


    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